Syntax is very important when writing code. A computer needs
exact instructions. That is why every
computer language has a syntax. The
syntax is a set of rules that you must follow in order for a command to execute
successfully. Today we are going to
examine the syntax section of a PowerShell Help File. Execute this command:
Get-Help Stop-Service
Below is the Syntax section.
SYNTAX
Stop-Service [-InputObject]
[-Exclude []]
[-Force] [-Include []]
[-InformationAction {SilentlyContinue |
Stop | Continue | Inquire | Ignore |
Suspend}] [-InformationVariable
[]] [-NoWait]
[-PassThru] [-Confirm] [-WhatIf]
[]
Stop-Service [-Exclude []]
[-Force] [-Include []]
[-InformationAction {SilentlyContinue |
Stop | Continue | Inquire | Ignore
| Suspend}] [-InformationVariable
[]] [-NoWait] [-PassThru]
-DisplayName [-Confirm]
[-WhatIf] []
Stop-Service [-Name]
[-Exclude []] [-Force] [-Include
[]] [-InformationAction
{SilentlyContinue | Stop | Continue |
Inquire | Ignore | Suspend}]
[-InformationVariable []]
[-NoWait] [-PassThru] [-Confirm] [-WhatIf]
[]
The Syntax block of information shows you all of the valid
parameters for Stop-Service and a
little bit about how to use them. Personally, I am not a fan of this cluster of
information. There is a lot going on
here. You can see that Stop-Service is listed 3 times. That is because the author of the cmdlet
created parameters that cannot be used at the same time. Download my code for Get-RequiredParameters (http://mctexpert.blogspot.com/2015/12/finding-required-parameters-in.html). Once you load Get-RequiredParameters into memory, execute this code:
Get-RequiredParameters
-Cmdlet Stop-Service | Where Required -eq $True
PS C:\>
Get-RequiredParameters -Cmdlet Stop-Service | Where Required -eq $True
Parameter Required Set
--------- -------- ---
InputObject true InputObject
DisplayName true Default
Name true
DisplayName
You are now looking at the parameter sets and the individual
parameters that cannot be used at the same time. Below is the syntax block of the help file
once again with these 3 parameters highlighted.
SYNTAX
Stop-Service [-InputObject]
[-Exclude []]
[-Force] [-Include []]
[-InformationAction {SilentlyContinue |
Stop | Continue | Inquire | Ignore |
Suspend}] [-InformationVariable
[]] [-NoWait]
[-PassThru] [-Confirm] [-WhatIf]
[]
Stop-Service [-Exclude []]
[-Force] [-Include []]
[-InformationAction {SilentlyContinue |
Stop | Continue | Inquire | Ignore
| Suspend}] [-InformationVariable
[]] [-NoWait] [-PassThru]
-DisplayName []> [-Confirm]
[-WhatIf] []
Stop-Service [-Name] [-Exclude
[]] [-Force] [-Include
[]] [-InformationAction
{SilentlyContinue | Stop | Continue |
Inquire | Ignore | Suspend}]
[-InformationVariable []]
[-NoWait] [-PassThru] [-Confirm] [-WhatIf]
[]
Information is also displayed with a series of <> and
[]. All of these items have the name of
a parameter (starts with a dash character “ – “) and then is followed by its
data type. There are four common combinations
of how to display this information utilizing the <> and []. They all mean different things.
[-ParameterName]
Square braces surrounding the parameter name and then
followed by the data type are required parameters. If you omit these parameters, PowerShell will
suspend execution and ask you for the value to give this parameter.
[-ParameterName ]
Square braces surrounding both the parameter name and data
type is an optional parameter. This
means that you use it only if you need to.
[-Parameter Name]
A parameter name in square braces with no data type is a
switch parameter. A switch parameter
does not accept an argument. You use
this to turn on extra functionality of the cmdlet.
[-ParameterName {Value1 | Value2 | Value 3}]
A parameter name inside of square braces with a set of
potential values inside curly braces is an optional parameter. The valid values for that parameter is
listed. Inside of the curly braces.
Tomorrow we will look at detailed help files.
Comments