Skip to content

Select-object expandproperty – select all matches in a string


reading the posting on getting back all matches in a string with

"1 2 3 4 5" | Select-String -AllMatches -Pattern '\d' | Select-Object -ExpandProperty Matches |  Select-Object -ExpandProperty Value

I became curious on what the -expandproperty does. The help says it resolves an array, to its values. Objects are resolved to their properties. When the property is an object and not expanded, you get back a “selected….”. With an expandproperty parameter you get a system.datetime. This can be useful, when the propery needs further processing or formatting:

PS C:\> get-childitem | Select-Object -First 1 lastaccesstime | gm    TypeName: Selected.System.IO.DirectoryInfo Name           MemberType   Definition ----           ----------   ---------- Equals         Method       bool Equals(System.Object obj) GetHashCode    Method       int GetHashCode() GetType        Method       type GetType() ToString       Method       string ToString() LastAccessTime NoteProperty System.DateTime LastAccessTime=19.05.2011 02:49:00 PS C:\> get-childitem | Select-Object -First 1 -expandproperty lastaccesstime | gm     TypeName: System.DateTimeName                 MemberType     Definition ----                 ----------     ---------- Add                  Method         System.DateTime Add(System.TimeSpan value) AddDays              Method         System.DateTime AddDays(double value) AddHours             Method         System.DateTime AddHours(double value) AddMilliseconds      Method         System.DateTime AddMilliseconds(double value) AddMinutes           Method         System.DateTime AddMinutes(double value) AddMonths            Method         System.DateTime AddMonths(int months) AddSeconds           Method         System.DateTime AddSeconds(double value) AddTicks             Method         System.DateTime AddTicks(long value)  

What can this be useful for ? The select string provides a hidden object called matches. “Matches” has all hits, their position and length.

Expanding the value parameter creates a list of the matched text

PS C:\> "a 12 string 3 and 4"| Select-String -Pattern '\d' -AllMatches a 12 string 3 and 4 PS C:\> "a 12 string 3 and 4"| Select-String -Pattern '\d' -AllMatches | Select-Object -ExpandProperty matches Groups   : {1} Success  : True Captures : {1} Index    : 2 Length   : 1 Value    : 1 Groups   : {2} Success  : True Captures : {2} Index    : 3 Length   : 1 Value    : 2 Groups   : {3} Success  : True Captures : {3} Index    : 12 Length   : 1 Value    : 3 Groups   : {4} Success  : True Captures : {4} Index    : 18 Length   : 1 Value    : 4 PS C:\> "a 12 string 3 and 4"| Select-String -Pattern '\d' -AllMatches | Select-Object -ExpandProperty matches| Select-O bject -ExpandProperty value 1 2 3 4 PS C:\>

Now think of the input is a webpage and you are grabbing email or other stuff you can describe with RegEx’s.

 Another thing is that you can define “filter” functions that make use of it.

$addresspattern = '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b'
$text = "The first mail address is and the second is"
filter Matches($pattern) {
$_ | Select-String -AllMatches $pattern | 
Select-Object -ExpandProperty Matches | 
Select-Object -ExpandProperty Value
$text | Matches $addresspattern 

See the original examples on ()
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: