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 Pointers
Tidbits on managing Windows Administration using PowerShell.
Tuesday, July 31, 2018
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.
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
("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.
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.
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.
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.
"winner " * 3
Note the trailing space at the end of the string. Without it, the text would all run together in an unbroken line.
Subscribe to:
Posts (Atom)