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 for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.9K Reputation points MVP Volunteer Moderator
    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.