fbpx Search a String for Text or a Pattern in Windows PowerShell | Windows PowerShell, Windows Server | HostingUltraso.com
24/7/365 Support

Search a String for Text or a Pattern in Windows PowerShell

Problem

You want to determine if a string contains another string, or want to find the position of a string within another string.

Solution

PowerShell provides several options to help you search a string for text.

Use the –like operator to determine whether a string matches a given DOSlike wildcard:

PS >"Hello World" –like "*llo W*"

True Use the –match operator to determine whether a string matches a given regular expression:

PS >"Hello World" –match '.*l[lz]o W.*$' True

Use the Contains() method to determine whether a string contains a specific string:

PS >"Hello World".Contains("World")

True

Use the IndexOf() method to determine the location of one string within another:

PS >"Hello World".IndexOf("World")

6

Discussion

Since PowerShell strings are fully featured .NET objects, they support many stringoriented operations directly. The Contains() and IndexOf() methods are two examples of the many features that the String class supports.

Although they use similar characters, simple wildcards and regular expressions serve significantly different purposes. Wildcards are much more simple than regular expressions, and because of that, more constrained. While you can summarize the rules for wildcards in just four bullet points, entire books have been written to help teach and illuminate the use of regular expressions.

Acommon use of regular expressions is to search for a string that spans multiple lines. By default, regular expressions do not search across lines, but you can use the singleline (?s) option to instruct them

to do so:

PS >"Hello `n World" match "Hello.*World" False PS >"Hello `n World" match "(?s)Hello.*World" True

Wildcards lend themselves to simple matches, while regular expressions lend themselves to more complex matches.

One difficulty sometimes arises when you try to store the result of a PowerShell command in a string, as shown in Example 53.

Example 53. Attempting to store output of a PowerShell command in a string

PS >GetHelp GetChildItem

NAME GetChildItem

SYNOPSIS Gets the items and child items in one or more specified locations.

(...)

PS >$helpContent = GetHelp GetChildItem PS >$helpContent match "location" False

The –match operator searches a string for the pattern you specify but seems to fail in this case. This is because all PowerShell commands generate objects. If you don’t store that output in another variable or pass it to another command, PowerShell converts to a text representation before it displays it to you. In Example 53, $helpContent is a fully featured object, not just its string representation:

PS >$helpContent.Name

GetChildItem

To work with the textbased representation of a PowerShell command, you can explicitly send it through the OutString cmdlet. The OutString cmdlet converts its input into the textbased form you are used to seeing on the screen:

PS >$helpContent = GetHelp GetChildItem | OutString

PS >$helpContent match "location"

True

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say