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
The one above only runs for "One File" only, but you can also run this for multiple files within your folder:
Credit to @rominator007
I wrapped it into a function (because you may want to use it again)
NOTE: This is NOT case sensitive!!!!!
See this post: String.Replace ignoring case
Note the parentheses around
(Get-Content file.txt)
is required:Use (V3 version):
Or for V2:
I prefer using the File-class of .NET and its static methods as seen in the following example.
This has the advantage of working with a single String instead of a String-array as with Get-Content. The methods also take care of the encoding of the file (UTF-8 BOM, etc.) without you having to take care most of the time.
Also the methods don't mess up the line endings (Unix line endings that might be used) in contrast to an algorithm using Get-Content and piping through to Set-Content.
So for me: Fewer things that could break over the years.
A little-known thing when using .NET classes is that when you have typed in "[System.IO.File]::" in the PowerShell window you can press the Tab key to step through the methods there.
You could try something like this: