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
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: get-Childitem c: \windows -recurse
The technique is to add the parameter directly after the main command. Remember to introduced your parameter or switch 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.
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 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:
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)
For example, to see a list of cmdlets that support remoting try:
get-command | where { $_.parameters.keys -contains "ComputerName"}
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: SolarWinds Engineer's Toolset v10
The Engineer's Toolset v10 provides a
comprehensive console of utilities for troubleshooting computer problems. Guy says
it helps me monitor what's occurring on the network, and the tools
teaches me more about how the system literally operates.
There are so many good gadgets, it's like having free rein of a
sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.
Download your copy of the Engineer's Toolset v 10
Second series of useful
PowerShell -Switches
-path (Example of an optional or assumed parameter)
-filePath (Variation on -path, used with Out-file)
-replace (select-String)
-pattern (select-String)
-descending (sort-Object)
-value (add-Content)
-newest (get-Eventlog)
-last (Win32_Process pageFaults -last 5)
-computerName (Useful scripts requiring loops)
-computer (works equally well in most scripts)
Guy Recommends: SolarWinds Engineer's Toolset v10
The Engineer's Toolset v10 provides a
comprehensive console of utilities for troubleshooting computer problems. Guy says
it helps me monitor what's occurring on the network, and the tools
teaches me more about how the system literally operates.
There are so many good gadgets, it's like having free rein of a
sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.
Download your copy of the Engineer's Toolset v 10
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.
Please write in if you see errors of any kind. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
Guy
Recommends: Orion's NPM - Network Performance Monitor
Orion's performance monitor is designed for detecting network outages.
A network-centric
view make it easy to see what's working, and what needs your attention.
This utility guides you through troubleshooting by indicating whether the
root cause is faulty equipment or resource overload.