PowerShell04 – Pipeline, Filtering, Grouping and Sorting examples

Pipeline:
PowerShell pipeline takes the output from one command and sends it as input
to another command.

Simple Example:

Get-Process | Select name,ID,CPU
Get-Date | Get-Member -MemberType Property

Sorting CmdLet results:
Sort-Object -Property XXX can be used to sort the results in output

Example to sort process information based on CPU in descending order:

Get-Process | Sort-Object -Property CPU -Descending
Get-Service | sort status, displayname

Grouping:
Group-object can be used to find the count of specific group

Example:

PS C:\Users\Vijay> Get-Service | Group-Object CanStop

Count Name                      Group
----- ----                      -----
   72 True                      {AdobeARMservice, AudioEndpointBuilder, Audiosrv, BFE...}
  124 False                     {AeLookupSvc, ALG, AppIDSvc, Appinfo...}

Filtering:
Where-Object can be used to filter the results based on specific condition

Example to find all processes taking more than 100 MB:

PS C:\Users\Vijay> Get-Process | Where-Object vm -gt 1000MB

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
   1939     119   112668     160580  1191   103.89   7240 explorer
    724      44    10308      19456  1264            1324 svchost
    418      26    11884      14912  1391            3360 taskhost

PS C:\Users\Vijay> Get-ChildItem "D:\Weekly Reports" | Where LastWriteTime -gt 10/15/2014
    Directory: D:\Weekly Reports
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        10/20/2014   3:35 PM      60112 Status WE 20141017.docx
-a---        10/27/2014   5:01 PM      59775 Status WE 20141024.docx
-a---        10/31/2014   7:56 PM      60026 Status WE 20141031.docx
Advertisements
This entry was posted in Learn_Powershell, Powershell and tagged , , , , . Bookmark the permalink.

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