none
Search for "*****" in files and change RRS feed

  • Question

  • I am having problems  changing content of files containing a string of , "*****". Throws error that the expression is not valid.

    My code is as follow -

    $items = Get-ChildItem * -recurse
    foreach ($item in $items)
    {
           if ($item.Attributes -ne "Directory")
           { 
             (Get-Content $item.FullName ) |
              Foreach-Object { $_ -replace '*****', 'xxxxx' } |
              Set-Content $item.FullName
            }
       }


    Monday, July 24, 2017 6:47 PM

Answers

  • Use "\*\*\*\*\*" for the replace string.

    Ed Crowley MVP "There are seldom good technological solutions to behavioral problems."
    Celebrating 20 years of providing Exchange peer support!

    • Marked as answer by wallacefalan Tuesday, July 25, 2017 12:53 PM
    Monday, July 24, 2017 8:44 PM

All replies

  • Please post your code correctly.  It is unreadable as posted.  Use the code posting tool as it will remove the bad formatting.

    \_(ツ)_/

    Monday, July 24, 2017 6:51 PM
  • just corrected - tks
    Monday, July 24, 2017 7:34 PM
  • You really need to use the code posting tool on the edit tool bar.  Code is hard to copy correctly when it is just pasted as text.

    If you are new to technical forums then look at some other posts to see what I mean.

    "-replace" uses Regex patterns.


    \_(ツ)_/


    • Edited by jrv Monday, July 24, 2017 7:39 PM
    Monday, July 24, 2017 7:39 PM
  • Use "\*\*\*\*\*" for the replace string.

    Ed Crowley MVP "There are seldom good technological solutions to behavioral problems."
    Celebrating 20 years of providing Exchange peer support!

    • Marked as answer by wallacefalan Tuesday, July 25, 2017 12:53 PM
    Monday, July 24, 2017 8:44 PM
  • Worked like a BOSS! Tks Ed.

    What's the logic behind this?

    Tuesday, July 25, 2017 12:54 PM
  • It's called "regular expressions".  Bing the phrase, and add the word "syntax" to learn more about how to create them.

    Ed Crowley MVP "There are seldom good technological solutions to behavioral problems."
    Celebrating 20 years of providing Exchange peer support!

    Tuesday, July 25, 2017 6:02 PM
  • Tks again Ed - will check it out
    Tuesday, July 25, 2017 7:42 PM