PowerShell for Windows Admins

Jun 2 2012   7:58AM GMT

Using a colon with cmdlet parameters

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Another question at Tuesdays PowerShell group revolved around using colons to link values to parameters. I’d not really thought about before. Thinking about it  later I realised that you only really see it when passing booleans to –Confirm.  If you don’t use a colon then you get this

PS> Disable-NetAdapter -Name "Virtual Wireless" -Confirm $false
Disable-NetAdapter : A positional parameter cannot be found that accepts argument ‘False’.
At line:1 char:1
+ Disable-NetAdapter -Name "Virtual Wireless" -Confirm $false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Disable-NetAdapter], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Disable-NetAdapter

But this will work

PS> Disable-NetAdapter -Name "Virtual Wireless" -Confirm:$false

Just for completeness this works as well

PS> Get-NetAdapter –Name:"Virtual Wireless" 

though it doesn’t tend to get used

2  Comments on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.
  • mconstable24
    Sorry, I don't understand:

    "Just for completeness this works as well

    PS> Get-NetAdapter –Name:"Virtual Wireless""

    The previous commands are to disable a Net Adapter, not get reference to it; I have never needed to substitute a space for a colon when supplying string variables to a cmdlet; and not using a colon to pass a string variable to the disable-netadapter seems to work also.

    What am I missing?
    0 pointsBadges:
  • aismaili
    The colon is required for boolean parameters.
    Reason is, if you set a boolean parameter like -Confirm or -Force, you already define it true. Another parameter behind it like $false is interpreted as a "next" parameter. Therefore, you need to tell PowerShell, that this $false or $true variable belongs to the boolean parameter with a colon. ;)
    0 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: