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.
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.
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.
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.
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.
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.
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.
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
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.