Using PowerShell, I want to replace all exact occurrences of [MYID]
in a given file with MyValue
. What is the easiest way to do so?
标签:
powershell
相关问题
- How to Debug/Register a Permanent WMI Event Which
- How can I do variable substitution in a here-strin
- How to use a default value with parameter sets in
- Does powershell have a method_missing()?
- Invoking Mirth Connect CLI with Powershell script
相关文章
- 在vscode如何用code runner打开独立的控制台窗口,以及设置好调试模式时窗口的编码?
- C#调用PowerShell的问题
- EscapeDataString having differing behaviour betwee
- PowerShell Change owner of files and folders
- Command line escaping single quote for PowerShell
- Is there a simple way to pass specific *named* Pow
- How do I access an element with xpath with a names
- How to 'Grant Permissions' Using Azure Act
Small correction for the Set-Content command. If the searched string is not found the
Set-Content
command will blank (empty) the target file.You can first verify if the string you are looking for exist or not. If not it will not replace anything.
After searching too much I figure out the simplest line to do this without changing the encoding is:
This is what I use, but it is slow on large text files.
If you are going to be replacing strings in large text files and speed is a concern, look into using System.IO.StreamReader and System.IO.StreamWriter.
(The code above has not been tested.)
There is probably a more elegant way to use StreamReader and StreamWriter for replacing text in a document, but that should give you a good starting point.
A bit old and different, as I needed to change a certain line in all instances of a particular file name.
Also,
Set-Content
was not returning consistent results, so I had to resort toOut-File
.Code below:
This is what worked best for me on this PowerShell version:
This worked for me using the current working directory in PowerShell. You need to use the
FullName
property, or it won't work in PowerShell version 5. I needed to change the target .NET framework version in ALL myCSPROJ
files.