1

I am having trouble finding how to find and replace in a file using poweshell. The file has several lines that are similar. I need to change the "ACT" to "RDY" in the line that starts with <PC_4 . All Lines start with < and end with >.

The Code:

powershell -Command(Get-Content C:\testfodler\test.XML) -replace '<PC_4 SET_THING="ACT">', '<PC_4 SET_THING="RDY">' | Out-File -encoding ASCII myFile.txt

The error I get is:

The '<' operator is reserved for future use.
Not all parse errors were reported.  Correct the reported errors and try again.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : RedirectionNotSupported

Running this on a windows 10 machine.

2
  • Read PowerShell.exe -? and follow the -Command section: To write a string that runs a Windows PowerShell command, use the format: "& {<command>}" where the quotation marks indicate a string and the invoke operator (&) causes the command to be executed… Commented Aug 2, 2021 at 17:23
  • @JosefZ - Sorry i'm out of my depth on this. Is the following what your saying to do? powershell -Command "&{(Get-Content C:\testfodler\test.XML)}" Commented Aug 2, 2021 at 17:51

1 Answer 1

2

First, you should quote the value (code) passed to the -Command parameter because it expects a string. How you quote depends on how you call powershell.exe.

If you are in PowerShell or CMD and are calling powershell.exe, you may surround the code passed to -Command with double quotes and double quote escape ("") inner double quotes, which is the PowerShell layer of escaping. Then backslash escape the escaping quote, which is for the cmd layer of escaping.

powershell.exe -command "(Get-Content C:\testfolder\test.XML) -replace '<PC_4 SET_THING=\""ACT\"">', '<PC_4 SET_THING=\""RDY\"">' | Set-Content myfile.txt -encoding ASCII"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.