how to use sort/where object

to sort the output use pipe “|”

Get-ChildItem | Sort-Object

Directory: C:\myScripts

Mode LastWriteTime Length Name
—- ————- —— —-
-a— 8/25/2012 2:35 PM 298 alias.txt
-a— 9/13/2012 8:18 PM 242 template.ps1
-a— 9/13/2012 10:37 PM 7610 ThreeAmigos.ps1

To override default property used by Sort-Object you need to specify the property:
Get-ChildItem | Sort-Object -Property length

Directory: C:\myScripts

Mode LastWriteTime Length Name
—- ————- —— —-
-a— 9/13/2012 8:18 PM 242 template.ps1
-a— 8/25/2012 2:35 PM 298 alias.txt
-a— 9/13/2012 10:37 PM 7610 ThreeAmigos.ps1

To narrow the query even further the Where-Object can be used:

Get-ChildItem | Sort | Where-Object { $_.Length -gt 242 }

Directory: C:\myScripts

Mode LastWriteTime Length Name
—- ————- —— —-
-a— 8/25/2012 2:35 PM 298 alias.txt
-a— 9/13/2012 10:37 PM 7610 ThreeAmigos.ps1

Advertisements

One thought on “how to use sort/where object

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