|
Guy recommends: This CG will put you in charge of controlling changes to network routers and other SNMP devices. Download your free Config Generator |
Windows PowerShell -Replace (Search and Replace)Introduction to Windows PowerShell -Replace ParameterThe 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 -ReplaceBefore 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. Topics for PowerShell -Replace String
♣ Example 1 Select-String -path -patternLet 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: Example 1a Select-String Using Variable $LocationRemember we are building up to replace string gradually. # Basic preparation just finding the text
Expected outcome:
:3: Means line number 3 Example of Select-String (Pure no extra commands) # Stage 2 See more about PowerShell Parameters
˚
Example 2 PowerShell -Replace (The Main Event)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
# Example of PowerShell -replace parameter 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. Note 3: See more about the Set-Content cmdlet. Note 4: See more about the Remove-Item cmdlet.
Guy Recommends: SolarWinds Engineer's Toolset v10
|
|||||
Download my ebook:
|
*
|
|
Guy
Recommends: WMI Monitor and Its Free!
|
|
Home Copyright © 1999-2010 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |