Powershell – Filtering


Filtering returned data

Where-Object
This cmdlet lets you filter data returned by other cmdlets.
To filter all commands that contain in the name the string export, type:

Get-Command | Where-Object {$_.name –like “*export*”}

Where-Object operators are:

Operator Description
-lt Less than
-le Less than or equal to
-gt Greater than
-ge Greater than or equal to
-eq Equal to
-ne Not equal to
-like Like; uses wildcards for pattern matching
-notlike Not like; uses wildcards for pattern matching
-contains Contains the specified value
-notcontains Doesn’t contain the specified value

Other parameters that can be used:

Parameter Description
-and And
-or Or

To get all svchost processes with handles bigger than 300, type

Get-Process | Where-Object {$_.hangles –gt 300 –or $_.name –eq “svchost”}

To get all files of folder scripts that contain in the name the string test and are bigger than 100Kb, type

Get-ChildItem c:\scripts | Where-Object {$_.name –like “*test*” –and $_.length –gt 100000}

Other option:

Get-ChildItem c:\scripts | Where-Object {$_.name.contains(“test”) –and $_.length –gt 100000}

Note: Where-Object alias is Where
 

In Depth
http://technet.microsoft.com/en-us/library/ee177028.aspx
http://www.howtogeek.com/138324/geek-school-learning-formatting-filtering-and-comparing-in-powershell/

Tested With
PowerShell 4.0

Advertisements
Categories: Powershell | Leave a comment

Post navigation

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: