Simple replace command but trying to replace a $ with the British pound sign

Anthony H 21 Reputation points
2021-09-13T16:29:30.043+00:00

Trying to replace the $ character using Power Shell Script. What to replace it with a £.
Instead of a £ I substituted a lower case z to keep it simple
PS M:\> $a = "My $ is not your $"
PS M:\> $a = $a -replace "`$","z"
PS M:\> $a
PS M:\> My $ is not your $z

I've tried various escape syntax and cannot seem to find the right combo.
I made sure the syntax works using non-special characters and it works like a charm but the $ seems to be problematic for me.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,427 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 99,371 Reputation points MVP
    2021-09-13T16:54:54+00:00

    Hi @Anthony H ,

    please try this:

    $a = 'My $ is not your $'  
    $a  
    $a = $a -replace "\$","z"  
    $a  
    

    Output looks like this PS5.1 or PS7.1 :

    My $ is not your $  
    My z is not your z  
    

    Another one:

    $a = 'My $ is not your $'  
    $a  
    $a = $a -replace "\$","€"  
    $a  
    # Output:  
    My $ is not your $  
    My € is not your €  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


0 additional answers

Sort by: Most helpful