Most of PowerShell's commands can be fine-tuned with -parameters,
otherwise known as switches. My aim of this page is to encourage you
keep a notebook of such useful PowerShell parameters.
To begin with, let me explain the basics with an example; here is a
command which searches just the top level container: Get-Childitem C:\windows\*
# PowerShell Parameter Example -recurse Get-Childitem C:\windows\* -recurse
Now when you add the -recurse parameter, the command takes longer to execute, but it's well worth the time to drill down the directory tree, and find the file that you are seeking in a sub-directory:
If you play
strategic games like chess, you may be familiar with the idea of
once you have found a good move, then look for an even better tactic. So it is with PowerShell, if you find a good command such as Get-Eventlog system, look for parameter to refine the output,
for example: Get-Eventlog system -newest 20.
Situation: You know the name of the cmdlet and want to investigate its
parameters
The key question is how did I know about the -newest parameter, as PowerShell calls this appendage? The answer is I called for help.
To be precise:
Clear-Host Get-help Get-Eventlog # Or better still:
Get-help -Eventlog -full
I recommend that you spend time studying the PARAMETERS section of
any cmdlet that you are working with, you are sure to unearth one unexpected
gem.
Situation: You know the parameters name, but want to find the
cmdlet(s) name
For example, to see a list of cmdlets that support remoting try:
# Find a PowerShell Parameter Get-Command | where {
$_.parameters.keys -contains "ComputerName"}
Modification to display just cmdlets with a particular PowerShell
parameter.
Clear-Host Get-Command| where { $_.parameters.keys -contains "ComputerName"}
` | where {$_.commandtype -contains "cmdlet"}
Note 1: Observe the backtick `. Also there should be no space after
that `.
Simplification employing PowerShell's -and :
# Research PowerShell Cmdlet Clear-Host Get-Command | where { $_.parameters.keys -contains "ComputerName" `
-and $_.commandtype -contains "cmdlet"}
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, Solarwinds
have created a
Free WMI Monitor so that you can discover these gems of performance
information, and thus improve your PowerShell scripts. Take the guess work out of which WMI counters to use when scripting the
operating system, Active Directory or Exchange Server.
Fantastic Script to Research PowerShell Parameters
My real-life task was to research which PowerShell parameters contained
-As, for example -AsHashTable. I posted my question in the
www.superuser.com forum, and this was
a marvellous reply from Joey:
# Research PowerShell Parameters by Joey Clear-Host Get-Command | where { $_.CommandType -eq 'Cmdlet' } | ForEach { $_ | `
Add-Member -PassThru NoteProperty AsParameters ($_.Parameters.GetEnumerator()
| ` Where {$_.Key -cmatch '^As([A-Z]|$)'} | ForEach { $_.Key }) } `
| Where { $_.AsParameters } | select Name, AsParameters
Note 2: The pattern I was looking for was 'As'.
Naturally, to research your parameters just substitute the text string you
are interested in.
If you digest every nuance of what Get-help says, then you discover that each Parameter has properties, e.g.: Required? True or False
Position: 1 or 2 etc.
Both logic and practical experience show that if a parameter is not required, and it's in position 1,
then you could safely omit it. In other words the parameter is assumed. Let us use Get-Childitem and -path as an example:
Thus Get-Childitem -path c:\windows could be reduced to plain Get-Childitem c:\windows.
(or even: gci c:\windows)
Another example of an assumed parameter comes from Get-Eventlog. This command will not work unless you help PowerShell by supplying the name of the log. The parameter
is -LogFile. For example Get-Eventlog -LogFile system. However, PowerShell understands that the first word after eventlog is the name the log, and thus we can omit the -Logfile parameter.
PowerShell assumes that the only possible parameter in this position is -LogFile and the following command completes successfully: Get-Eventlog system.
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
Solarwinds'
Orion performance monitor
will help you discover what's happening on your network. This
utility will also guide you through troubleshooting; the dashboard will
indicate whether the root cause is a broken link, faulty equipment or
resource overload.
Perhaps the NPM's best feature is the way it suggests solutions to network
problems. Its
second best feature is the ability to monitor the health of individual VMWare
virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you take advantage of Solarwinds' offer.
One source of confusion is that the same PowerShell parameter seems to have
two or more forms, for example: -ComputerName -computer or even -comp.
The explanation is that PowerShell's intelligence means that you only have
to use sufficient letters to provide a unique combination that cannot be
mistaken for any other parameter.
-foreground and -background example
# Abbreviating PowerShell Parameters $Date = Get-Date Write-Host
$Date -ForeGroundColor green -b magenta Write-Host $Date -fore yellow
-back green Write-Host $Date -f red -BackGroundColor black
The actual parameters are -ForegroundColor and -BackGroundColor, but as you
can see, we can shorten them right down to -f and -b.
Guy's Second Series of Useful
PowerShell Parameters (-Switches)
-path (Example of an optional or assumed parameter) -filePath (Variation on -path, used with Out-file)
The technique is to add the parameter directly after the main command. Remember to introduced your parameter with a -minus sign, and not a backslash. If you apply the terminology
strictly, then the difference between a parameter and a switch is that a switch does not take a value, whereas a parameter can.
PowerShell also has a family of comparison or pattern matching switches. You may see these conditional operators such as -match and -like in 'Where' clauses, for example:
Get-wmiobject
-list | where {$_.name -match "Win32"}
®
This family of switches has several names, conditional operators and pattern matching switches, let me introduce the family:
-match
-like
-contains
Sometimes logic dictates that their negative cousins produce tighter code, for example: -notmatch
Although we are straying further from my original idea of switches to modify a command, I should
mention the logical operators as they too are introduced by a -dash.
-eq (Beware in PowerShell you cannot substitute an equals sign for -eq)
-ne (Not equal, incidentally -neq will not work)
-and
-or
There are also bitwise variants -bor -band. This is not an exhaustive list, there are exotic operators such as an exclusive or called -xor.
Summary of PowerShell's Parameters
Understanding PowerShell's parameters will improve your scripts. Firstly, you get extra capabilities, for example -recurse, secondly you greater precision, for example -MemberType. In
conclusion, never miss a chance to research a Parameter or a Switch, if you find a really good example, email me and I will add it to Guy's top 10 Switches.
If you like this page then please share it with your friends
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.