Parameter sets

Many of the commands in PowerShell have more than one parameter set. This was seen while looking at the syntax section of help; for example, Stop-Process has three parameter sets:

SYNTAX
Stop-Process [-Id] <Int32[]> [-Confirm] [-Force] [-PassThru] [-WhatIf] [<CommonParameters>]

Stop-Process [-InputObject] <Process[]> [-Confirm] [-Force] [-PassThru] [-WhatIf] [<CommonParameters>]

Stop-Process [-Confirm] [-Force] -Name <String[]> [-PassThru] [-WhatIf] [<CommonParameters>]

Each parameter set must have one or more parameters unique to that set. This allows each set to be distinguished from the other. In the previous example, Id, InputObject, and Name are used as differentiators.

The first parameter set expects a process ID, and this ID may be supplied with the parameter name or based on position; for example, both of these commands close the current PowerShell console:

Stop-Process -Id $PID 
Stop-Process $PID 

The second parameter set needs a value for InputObject. Again, this may be supplied as a positional parameter. In this case, it will be distinguished based on its type:

$process = Start-Process notepad -PassThru 
Stop-Process -InputObject $process 
Stop-Process $process 
$process | Stop-Process  
Pipeline input

Get-Help should help show which parameters accept pipeline input, and examples are likely to show how.

If Get-Help is incomplete, Get-Command can be used to explore parameters:
(Get-Command Stop-Process).Parameters.InputObject.Attributes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.241.82