Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell and dollar sign in argument

Let's assume my script is the following:

param([string]$password)
Write-Host $password

This will return if I launch it this way:

.\myDollarParam.ps1 -Password Pa$sword
 Pa

I would like the following output:

 Pa$sword

Is there a way to do that aside using ` or \ in the argument ?

Thanks.

like image 979
HelloToTheWorld Avatar asked Dec 16 '22 09:12

HelloToTheWorld


1 Answers

The easy way is to enclose it in single quotes. For ex.:

.\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:

$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.

like image 179
HAL9256 Avatar answered Dec 23 '22 09:12

HAL9256