Introduction to Windows PowerShell -Replace Parameter
The first point to note with -Replace is that it is a parameter or a switch, therefore
it needs input from another
PowerShell cmdlet. One way of preparing a text string for such a search and replace operation is by
using Get-Content.
Introduction to: Select-String -Replace
Before you call for the -replace parameter, first you need to obtain stream of text.
Once you connect to the input stream, then define the pattern that you are seeking. As you are reading this I expect you are thinking of
possible applications for this technique. Perhaps you wish to find which documents contain a particular string, alternatively, you may be trying a more complex search and replace operation. My
point is -replace maybe a bit-part in a bigger drama.
Let us build up slowly, before we use the -replace parameter, let us have a refresher on
Select-String and Get-Content.
The key to understanding Select-String is studying the two main switches -path and -pattern. They say to me 'Where is the input?' and 'What pattern do you want to match?'.
To ensure that my examples work, we need to agree on the file location and the pattern to search.
Thus for 'our' script to work you need to embrace one of two tactics, either mimic my folder structure and
patterns, or amend the script to fit in with your environment.
My folder happens to be called : D: \powershell\stuff.
Here are two simple Select-String scripts which produce the same result
using slightly different methods. By studying both you will gain both perspective and ideas for the best method for your scripts.
Assumptions: You have a file called gopher.txt. In gopher.txt is the
the word Guido.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, Solarwinds
have created a
Free WMI Monitor so that you can discover these gems of performance
information, and thus improve your PowerShell scripts. Take the guess work out of which WMI counters to use when scripting the
operating system, Active Directory or Exchange Server.
My idea in Example 1b is to build on Select-String, now, at last, we are
ready to replace
one string with another. Here is a common problem a repeated word
"the the". What I want to do is replace the double word with a
single instance.
Replace Rationale
Get-ChildItem locates all files beginning with 'g'. Note
the path.
Get-Content reads the files
A second foreach construction deals with the -replace "the the"
operation.
# Example of PowerShell -replace parameter clear-Host $file = Get-ChildItem "D:\powershell\snippets\g*.txt" foreach ($str in $file) {
$content = Get-Content -path $str $content | foreach {$_ -replace "the the", "the"} |
Set-Content $str
} write-Host "After replace `n" $file
Note 1: The purpose of `n is to force a carriage return.
Note 2: Troubleshooting. Remember this is an
example, you need to create your own file, and you probably need to change
the value of $file to suit your computer.
Import users from a spreadsheet. Just provide a list of the
users with their fields in the
top row, and save as .csv file. Then launch this FREE utility and match
your fields with AD's
attributes, click to import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
Clear-Host Get-Command | where { $_.parameters.keys -contains
"replace"}
Much to my surprise the only cmdlet to feature -replace is
Update-List.
Help update-List
-Replace <Object[]> Specifies a new collection. This parameter
replaces all items in the original collection with the items specified
by this parameter.
This is a classic example of building
a script gradually. Master the basics of
Select-String, and only then focus on the -replace parameter.
Remember that firstly first you need to obtain stream of text. Once you
connect to the input stream, then define the pattern that you are seeking.
If you like this page then please share it with your friends
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.