Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape "<" character in password inside PowerShell script

Tags:

powershell

Here's my script:

@rasdial "My VPN" "user@domain" 'my<password'

My password contains < character. This, without @ works when entered into PowerShell console.

I know ` is the quoting character, but obviously it doesn't work with <. My password contains many special characters so it has to be quoted. But it doesn't work when double quotes are used. How to escape the password properly? Is there a way to import it from an external file? BTW, rasphone.exe remembers my password, maybe is there a way to use it?


Here's the solution I found working. I used double quotes instead of single quotes. It didn't work at the first time, because there was also % in my real password, which needed to be quoted with %%.

So, inside .cmd script: 'my%`<password' won't work, but "my%%<password" will.

Can anyone confirm he used string like '...`<...' inside .cmd script and it worked? Does it behave differently in different PowerShell versions? Mine is from Windows 8 x64.

like image 317
Harry Avatar asked Dec 05 '25 00:12

Harry


2 Answers

I had a similar issue and it took me a while to get to the bottom of it. In my case I had a password similar to this

}K)/]j..|{?*&%($@7}e$%0>;._#

Putting it in double quotes allows for variable expansion so obviously that wont work. Putting it in single quotes is supposed to work but didn't for me. Escaping problem characters with the back tick also didnt work for me.

What worked for me was to surround with single quotes first and then double quotes

'"}K)/]j..|{?*&%($@7}e$%0>;._#"'

like image 161
SpeedOfSpin Avatar answered Dec 06 '25 16:12

SpeedOfSpin


Have you tried:

'my`<password'

` <--- The tilde key backquote is the Powershell escape character.

I would also encourage using -AsSecureString

Testing in the PS console:

PS C:\Users\Athomsfere> $pw = 'my<password'
PS C:\Users\Athomsfere> write-host $pw
my<password

So its not the character by itself.

It still woks like this too: (As a script)

'@rasdial', "My VPN", "user@domain", 'my<password' |`
    ForEach
    {
        Write-Host $_
    }
like image 30
Austin T French Avatar answered Dec 06 '25 14:12

Austin T French



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!