locked
Need to replace text in a file that contains $ in it. RRS feed

  • Question

  • I am trying to write a script to find all *.config files within a specific directory and subdirectories and if the file contains

    Password=3v6Q$T359P   change it to Password = 123456   The issue I have found is if the text contains  $ it does not replace the text.   Here is what I have so far.  

    $old ="Password=3v6Q$T359P"
    $new = "Password=123456"
    get-childitem c:\test -recurse -include *.config |
     select -expand fullname |
      foreach {
                (Get-Content $_ ) -replace $old, $new |
                 Set-Content $_
                }

    Tuesday, December 5, 2017 6:18 PM

Answers

  • made the change and still not working

    $old ='Password=3v6Q\$T359P'
    $new = 'Password=123456'
    get-childitem c:\test -recurse -include *.config |
     select -expand fullname |
      foreach {
                (Get-Content $_ ) -replace $old, $new |
                 Set-Content $_
                }

    • Marked as answer by NORMANWZACHER Tuesday, December 5, 2017 7:46 PM
    Tuesday, December 5, 2017 7:36 PM

All replies

  • Try single-quotes when declaring $old

    $old ='Password=3v6Q$T359P'


    Jeremy Corbello | https://www.jeremycorbello.com

    Tuesday, December 5, 2017 6:26 PM
  • $old ='Password=3v6Q\$T359P'

    Escape it and don't use double quotes on patterns.


    \_(ツ)_/

    Tuesday, December 5, 2017 6:26 PM
  • Didn't help
    Tuesday, December 5, 2017 7:35 PM
  • made the change and still not working

    $old ='Password=3v6Q\$T359P'
    $new = 'Password=123456'
    get-childitem c:\test -recurse -include *.config |
     select -expand fullname |
      foreach {
                (Get-Content $_ ) -replace $old, $new |
                 Set-Content $_
                }

    • Marked as answer by NORMANWZACHER Tuesday, December 5, 2017 7:46 PM
    Tuesday, December 5, 2017 7:36 PM
  • It works perfectly:

    'Password=3v6Q$T359P' -replace 'Password=3v6Q\$T359P','Password=123456'

    You have an XML file and you need to use the XML object to edit it as XML can obfuscate the text.


    \_(ツ)_/


    • Edited by jrv Tuesday, December 5, 2017 7:41 PM
    Tuesday, December 5, 2017 7:40 PM
  • $old = 'Password=3v6Q\$T359P'
    $new = 'Password=123456'
    get-childitem c:\test\*.config -recurse |
        ForEach-Object {
            $f = Get-Content $_ | %{$_ -replace $old, $new}
            $f | Set-Content $_
        }


    \_(ツ)_/

    Tuesday, December 5, 2017 7:44 PM
  • Never mind.  Its working.. Thanks!
    • Marked as answer by NORMANWZACHER Tuesday, December 5, 2017 7:46 PM
    • Unmarked as answer by NORMANWZACHER Tuesday, December 5, 2017 7:46 PM
    Tuesday, December 5, 2017 7:46 PM
  • jrv,

    This is my confusion with these here:

    'Password=3v6Q$T359P' -replace 'Password=3v6Q\$T359P','Password=123456'
        Password=123456
    $old.Replace('Password=3v6Q\$T359P','Password=123456')
        Password=3v6Q$T359P
    $old.Replace('Password=3v6Q$T359P','Password=123456')
        Password=123456
    Replacing via the escape character doesn't seem to work if the string is saved in a variable.

    Jeremy Corbello | https://www.jeremycorbello.com



    • Edited by Jacorbello Tuesday, December 5, 2017 7:49 PM
    Tuesday, December 5, 2017 7:48 PM
  • The $ is a special character and must be escaped when  using a match pattern.

    It works as expected you just can't use the pattern to replace the pattern.

    ('Password=3v6Q$T359P').Replace('Password=3v6Q$T359P','Password=123456')


    \_(ツ)_/

    Tuesday, December 5, 2017 8:03 PM
  • You marked your complaint as an answer.  That is not very helpful to others.


    \_(ツ)_/

    Tuesday, December 5, 2017 8:04 PM