Question:
Let’s assume my script is the following:
1 2 3 |
param([string]$password) Write-Host $password |
This will return if I launch it this way:
1 2 3 |
.\myDollarParam.ps1 -Password Pa$sword Pa |
I would like the following output:
1 2 |
Pa$sword |
Is there a way to do that aside using ` or \ in the argument ?
Thanks.
Answer:
The easy way is to enclose it in single quotes. For ex.:
1 2 |
.\myDollarParam.ps1 -Password 'Pa$sword' |
Otherwise PowerShell interprets the “$” as the start of a variable, so what you are basically saying in your example, is assign the variable the value “Pa” and then the value stored in the variable “$sword”
A to prove this theory running the following gives me the interesting result:
1 2 3 4 |
$sword = "Stuff" .\myDollarParam.ps1 -Password Pa$sword PaStuff |
By enclosing in single quotes, you are saying pass the literal characters in this string.
The “proper” way to do passwords is to use the ConvertTo-SecureString cmdlt.