Subscribe to Windows IT Pro
November 19, 2010 12:01 PM

Replacing Strings in Files Using PowerShell

Script fills void left from missing cmdlet
Windows IT Pro
InstantDoc ID #126454
Rating: (0)
Downloads
126454.zip

Windows PowerShell provides native capabilities for string pattern matching and string replacement through its comparison operators. However, PowerShell doesn't have a native cmdlet for replacing strings of text in files, so I wrote the Replace-FileString.ps1 script to fill this void. After I show you how to take advantage of PowerShell's native capabilities, I'll show you how to use Replace-FileString.ps1.

PowerShell's Native Capabilities

PowerShell's -like, -match, and -replace operators make matching and replacing strings much more accessible to nonprogrammers. PowerShell's command line provides users with the ability to experiment with .NET regular expressions with greater ease than with traditional scripting languages. A regular expression (sometimes referred to as regex) is a string that contains special characters or character sequences that represent other characters or character sequences. Regular expressions are similar to wildcard patterns but are much more expressive. If you're not familiar with regular expressions, the PowerShell Help topic about_regular_expressions can help you get started learning about them. To view the help topic, type

Get-Help about_regular_expressions

at a PowerShell prompt.

An obvious application for regular expressions is to replace strings in files. For example, suppose you want to extract only the computer names from the output of the Net View command. If you run the command

Net View > List.txt

then open the List.txt file in Notepad, you'll notice all kinds of extraneous information such as column headings, extra spaces at the end of each line, and a footer line that tells you the command completed successfully. Using regular expressions in PowerShell, you can extract and output only the computer names. Take, for example, the command

 

Get-Content List.txt |

  Where-Object { $_ -match '^\\\\' } |

  ForEach-Object { $_ -replace

  '^\\\\(\S+).+','$1' }

(Although this command wraps here, you'd enter it all on one line in the PowerShell console. The same holds true for the other commands that wrap.) The Get-Content cmdlet retrieves each line in List.txt. The Where-Object cmdlet then uses the -match operator to check whether each line starts with two backslashes. If a line starts with two backslashes, it's passed to ForEach-Object cmdlet, which uses the -replace operator to output only the computer names.

Let's take a closer look at the -match operator, which is used with the pattern ^\\\\. The caret symbol (^) indicates that you want to match the beginning characters. Because the backslash is the escape character for regular expressions, you have to use two backslashes (\\) to represent a single backslash (\). Thus, you need to include a total of four backslashes. Table 1 describes this and other regular expression patterns used in this command.

Replace-FileString.ps1

When working with text files, it's often useful to be able to replace strings in a file using regular expressions, then write the results back to the original file. Because there isn't a PowerShell cmdlet that provides this functionality, I wrote Replace-FileString.ps1. It's a PowerShell equivalent to opening a file with Notepad, doing a find-and-replace operation, and saving the file. But unlike Notepad, you can use this script to replace strings in multiple files at the same time.

Replace-FileString.ps1 supports searching across line breaks because it uses the ReadAllText method of the Windows .NET Framework's System.IO.File class instead of the Get-Content cmdlet to read in text. Unlike Get-Content, which reads only one line of a file at a time, the ReadAllText method reads each file as a single string, so it can find strings across line breaks. (Because each file is one string, the script might run slow for very large files.)

Replace-FileString.ps1 requires PowerShell 2.0. The script's command-line parameters are listed in Table 2. In addition to the parameters listed in Table 2, the script also supports the -Confirm, -Verbose, and -WhatIf common parameters.

 

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.