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 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 Select-string (Guy's indulgence)

My main idea in Example 1c is to introduce an If... Else clause to cater for instances where the -pattern cannot be found.  To prepare for the If logic I have introduced another variable $SearchStr.  The second time you run this script you may wish to find and amend the "zzz" to a value that will ensure success.

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

Note 1:  The If test: If ($Sel -eq $null)  What this says if the value returned by Select-string is nothing ($Null).  Incidentally, there are two lls in $null.  The correct syntax is -eq and not plain old =.

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

  ˚

Summary of -Replace

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

See more Microsoft PowerShell syntax

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

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

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

Please report a broken link, or an error.