PowerShell's Top Ten Parameters (Switches)PowerShell's Top Ten Parameters, or -Switches
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:
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: Topics for PowerShell's Parameters
Guy's Top Ten PowerShell -Parameters
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.
˚
How to research more PowerShell Parameters or -SwitchesIf 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. The key question is how did I know about the -newest parameter, as PowerShell calls this appendage? The answer is I called for help. get-help get-Eventlog. Or better still: get-help -Eventlog -full. I recommend that you spend time studying the PARAMETERS section. The Concept of an Optional, or an Assumed ParameterIf you digest every nuance of what get-help says, then you discover that each Parameter has properties, e.g.: 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: get-Childitem -path c:\windows get-Help get-Childitem PARAMETERS Thus get-Childitem - path c:\windows could be reduced to plain 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. Second series of useful PowerShell -Switches
Pattern Matching SwitchesPowerShell 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:
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.
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 ParametersUnderstanding 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. See Also PowerShell Tutorials:• PowerShell Home • Com • Shell Application • Active Directory • QAD Snap-in • Get-Member 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.
*
|
||||||