Windows PowerShell


Windows PowerShell -Replace (Search and Replace)

Introduction to Windows PowerShell Select-string

The first point to note with -Replace is that it is a parameter or a switch, therefore needs preceding command.   One way of preparing a text string for a search and replace operation is by using get-content.

Introduction to: Select-string

To begin with you need to obtain stream of text, once you have the path to the input, then you need to define the pattern that you are seeking.  As you are reading this I expect you are thinking of possible application for this command.  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 that Select-string may be merely a bit-part in a bigger drama.  What ever you use for Select-string it's worth mastering the basics and being aware of the options.

Topics for PowerShell Select-string

 ♣

Example 1 Select-string -path -pattern

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.  To be successful 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.  What is even more important is that I navigate in powershell to the folder where I store the files with the patterns I am testing.

Here are three simple scripts which all produce the same result using slightly different methods.  By studying all three 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.

Example 1a Select-string using variable $Location

$Location = "D:\powershell\stuff\gopher.txt"
select-string -path $Location -pattern "Guido"

Expected outcome:
D:\powershell\stuff\gopher.txt:3:Guido is king

:3:  Means line number 3
:Guido is king  Refers to the line where the Pattern "Guido" occurs.

Example 1b Select-string (Pure no extra commands)

select-string -path "D:\powershell\stuff\gopher.txt" -pattern "Guido"

Example 1c -replace (Guy's indulgence)

My main idea in Example 1c is to build on select-string and now 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.

clear-Host
$file = gci "D:\powershell\snippets\g*.txt"
$file
foreach ($str in $file)
{
$cont = get-content -path $str
$cont | foreach {$_ -replace "the the", "the"} | set-content $str
}
write-host "After `n"
$file

Note 1: The purpose of `n is to force a carriage return.

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and the tools teaches me more about how the system literally operates.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

Summary of -Replace

This is a classic example of building a script gradually.  Master the basics of select-string, and only then focus on the -replace parameter.

See more PowerShell examples for syntax advice

PowerShell Home  • Syntax  • -f format  • Pipeline  • Quotes  • Format-table  • Group  • Select-String

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.