Search code examples
powershellpowershell-5.0

String Only For oddly formatted password


Ok, I have a lot of passwords that are oddly formatted that needs to be passed a raw string. I know to use the ' to force any $ from activating. However, some of these passwords have ' inside of them. So, How would I go about raw stringing them data? Example of one of the passwords I am struggling with:

|4255a7]'KbuSkaiC$^&#{/d}nV41L8#*B?,3pevv

Notice the $^ This will trigger "userpassword" to appear inside the script. If I use a ' at the front, it stops at the ' before the K. Any ideas what to do?


Solution

  • Normally you'd escape special characters with a backtick. However for single quote just double it up.

    '|4255a7]''KbuSkaiC$^&#{/d}nV41L8#*B?,3pevv'
    

    Output

    |4255a7]'KbuSkaiC$^&#{/d}nV41L8#*B?,3pevv
    

    Note that if you're taking input from a user say with Read-Host - they do not need to escape it.

    $var = Read-Host -Prompt "Enter Password"
    Enter Password: |4255a7]'KbuSkaiC$^&#{/d}nV41L8#*B?,3pevv
    
    $var
    |4255a7]'KbuSkaiC$^&#{/d}nV41L8#*B?,3pevv