Tuesday, July 31, 2018

Use PowerShell to Change Browser Settings

Last time we saw how to get our Internet Explorer settings out of the registry. And in PowerShell, for nearly every 'Get', there's an equivalent usage for 'Set'. 

Here's how it works. Suppose we wanted to change, say, the Start Page for Internet Explorer. First, let's get the current setting (We'll use variables to minimize typing):

$path = 'HKCU:\Software\Microsoft\Internet Explorer\Main\'
$name = 'start page'
(Get-ItemProperty -Path $path -Name $name).$name

 


As we can see, the IE start page is currently set to msn.com

So where there's a get, there's a set:

$value = 'https://www.google.com/'
Set-ItemProperty -Path $path -Name $name -Value $value







Now we've set IE's start page to google.com. We can test this by running the Get-ItemProperty command again to get the setting, or simply launch IE and see what comes up.




Wednesday, July 25, 2018

Use PowerShell to Quickly Find Browser Settings

PowerShell can quickly display information from the Registry, provided we know the key's path. For example, here's how to find our Internet Explorer settings:

Get-Item 'HKCU:\Software\Microsoft\Internet Explorer\Main'

 


Of course, "HKCU" is short for "HKEY Current User". 'HKLM' would be short for "HKEY Local Machine", etc.

Monday, July 23, 2018

Use PowerShell to Get the Length of an Integer

PowerShell can return the length of a string easily. Wrap the string in parentheses and the the Length property of the object using dot notation:

("Some strings are rather lengthy").Length



But what about integers?

 

That's because the Length property is counting the integer as a whole rather than looking at the individual units. If we're going to say, "Hey PowerShell, use this string method to get the length of what's in between the parens", then PowerShell is going to assume it's a string, not an integer.

To get around this, we can just convert the integer to a string using the 'ToString()' method:

(6548138735138).ToString().Length

 

 
 

Sunday, July 22, 2018

Use PowerShell to launch Internet Explorer

Want to save some time? With PowerShell, we can use Start-Process to launch Internet Explorer:

Start-Process iexplore www.google.com

 


We can speed that up with an alias, substituting 's' for Start-Process:

New-Alias -Name s -Value Start-Process

 


We can test the alias with notepad:

s notepad

And  a blank notepad should open right up.

Now we should be able to launch Internet Explorer more quickly using our alias:

s iexplore https://en.wikipedia.org

 


Even better, we can create an alias for Internet Explorer itself. Let's call it 'ie':

New-Alias -Name ie -Value "C:\Program Files\Internet Explorer\iexplore.exe"

(Your system's path to the executable may vary.)

We can test the alias:

ie

and a new instance of Internet Explorer should launch.

If we put those aliases into our PowerShell profile, they'll always be available.

Note: aliases are a good time-saver for our own interactive sessions with PowerShell, but they are not recommended for use in scripts, since the aliases make it harder for others to know what the scripts are doing.

Saturday, June 30, 2018

Use PowerShell to Search Text Files for Strings

PowerShell can quickly search text files for a string of characters:

Select-String -Path .\servers.txt -Pattern "abc"

 


For this example, we used Select-String to search a list of server names in a text file called 'servers.txt', then used the -Pattern parameter to designate the string we're looking for.

PowerShell returned the filename, the line number where the pattern was found, and the full line. 

Note: case is not sensitive here. A search for "ABC" will return the same result. 
 

Use PowerShell to create a Zip archive

PowerShell can easily create zip archives of files and folders using the Write-Zip cmdlet.

Get-ChildItem C:\MyScripts\*.ps1 | Write-Zip -OutputPath C:\Temp\PS-scripts.zip

Here we zipped up all the PowerShell scripts written or downloaded for archive purposes:



Get-ChildItem also has a -Recurse parameter if we want to zip up subfolders, and the Write-Zip cmdlet has a -IncludeEmptyDirectories parameter if we want to, well, include empty directories.

Friday, June 8, 2018

Use PowerShell to Multiply Strings

Long, long ago, computers mastered the art of multiplying numbers. A more interesting trick is to multiply string. PowerShell can do this easily:

"winner " * 3







Note the trailing space at the end of the string. Without it, the text would all run together in an unbroken line.

 

Use PowerShell to send a Beep to the Console

PowerShell can send beeps to the console:

[console]::beep[500,300]

It's nice to throw two or three of these at the end of a long-running script to alert when the script is finished.

The first number is the pitch of the beep, and the second number is the duration of the beep in milliseconds.

Thursday, May 24, 2018

Check Your Administrator Status in PowerShell

Most of the time, a script we've written for PowerShell may work like this:



 But then occasionally, this happens:



Whoops. What happened?

One thing to double-check is, Does our script require administrative access to work, and are we running PowerShell as an Administrator?

Here's how to check that:

([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")

 
The command is returning False, so no, this PowerShell instance was not launched as Administrator.

Of course, that's a long, hard-to-remember command, which means it's an excellent candidate for a function:

Function Check-Admin 
{  # Check Admin rights
([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")  
}  

 Stick that function into our profile, and we'll never have to remember that ugly command again.

Thursday, May 17, 2018

Choose Random Numbers with PowerShell

Do we need to choose a number from 1 to 100?

Do we need to choose five random numbers from 1 to 100?

Do we have PowerShell?

Then we are all set.

First, create an array of numbers from 1 to 100:

$array = (1..100)

Then let PowerShell randomly choose 5 numbers from the array:

Get-Random -InputObject $array -Count 5




And if we need those results in numerical order?

Get-Random -InputObject $array -Count 5 | Sort-Object



 
   

Tuesday, May 15, 2018

PowerShell: Forcing Confirmation

In a previous post, we saw how to check the Confirmation Preference, and how it works compare to a cmdlet's built-in confirmation level. But if we want to force a cmdlet to run with confirmation, we can use the '-Confirm' switch.

Here's how that works:

 

Like last time, we launched an instance of calculator.exe, found the process ID using Get-Process, then killed it with Stop-Process. PowerShell didn't hesitate to do as we commanded. But for safety's sake, let's add the '-Confirm' switch:



This time we had to take an extra step before the process was stopped. This feature would be useful in, for example, an interactive script designed for newer users, who might not be as comfortable taking permanent or destructive actions.