powershell on command line replace double quoted text

Welcome Forums General PowerShell Q&A powershell on command line replace double quoted text

Viewing 3 reply threads
  • Author
    Posts
    • #163848
      Participant
      Topics: 1
      Replies: 1
      Points: 15
      Rank: Member

      Hi all

      I have a file containg (multiple) xml tags that contain the attribute type=”ABC”. The ABC needs to be replaced by XYZ.

      I have tried to use the below (escape with backtick, as it seems to be the standard in powershell) and the variation of it by escaping the double quotes with the backslash (I’m basically a pure C programmer).

       powershell -command "(gc %rootdir%\directory\file3.xml) -replace 'TYPE=`"ABC`"','TYPE=`"XYZ`"' | Out-File %rootdir%\directory\file.xml" -encoding ASCII

      I eventually tried escaping the double quote with another double quote and that worked. The question is where I can read up on escaping in powershell or if somebody can explain why only the escaping the double quote with a double quote worked.

      Thanks in advance

    • #163854
      Senior Moderator
      Topics: 9
      Replies: 1236
      Points: 4,443
      Helping Hand
      Rank: Community Hero
    • #163857
      Participant
      Topics: 5
      Replies: 2368
      Points: 5,987
      Helping Hand
      Rank: Community MVP

      You may read the free Powershell cookbook about working with XML files. I’d recommend to treat an XML file not like plain text. 😉

    • #163863
      Participant
      Topics: 1
      Replies: 1
      Points: 15
      Rank: Member

      Thanks people for the replies.

      I’m aware that I will probably be better of using the XML approach but this is an attempt at a quick and dirty hack to fix a tool-chain after requirements for the output files changed.

Viewing 3 reply threads
  • The topic ‘powershell on command line replace double quoted text’ is closed to new replies.