Purclot
Purclot

Reputation: 559

PowerShell ConvertTo-SecureString Syntax

I've got a problem understanding the syntax of the ConvertTo-SecureString cmdlet. From the help: ConvertTo-SecureString [-String] <String> [[-AsPlainText]] [[-Force]] [<CommonParameters>]. The single square bracket means: an optional parameter, right? And the double square brackets around -AsPlainText and -Force? Thanks for your help Purclot Avignon

Upvotes: 1

Views: 751

Answers (2)

user4003407
user4003407

Reputation: 22102

That means that AsPlainText and Force is optional positional switch parameters. So you can call it like this:

ConvertTo-SecureString 'Password' -AsPlainText -Force

like this:

ConvertTo-SecureString 'Password' $true $true

or like this:

ConvertTo-SecureString ${Value from ConvertFrom-SecureString command}

Parameter definition for mandatory named parameter look like this:

-ParameterName <ParameterType>

If parameter is optional, that adds square brackets around it:

[-ParameterName <ParameterType>]

If parameter is positional (parameter name optional), that adds square brackets around parameter name:

[-ParameterName] <ParameterType>

If parameter is switch parameter, that removes <ParameterType> part:

-ParameterName

So, when you have optional positional switch parameter it would be like this (two square brackets and no <ParameterType> part):

[[-ParameterName]]

And it happens that mandatory positional switch parameter and optional named switch parameter both looks like:

[-ParameterName]

Upvotes: 1

Rahul
Rahul

Reputation: 77846

No you got it wrong. [-String] <String> is not an optional parameter rather it's a required parameter. [-AsPlainText] and [-Force] are both optional parameter.

See documentation ConvertTo-SecureString specifically the Parameters section and in that see the value of Required? attribute.

Upvotes: 0

Related Questions