Easy sorting with Powershell

Say you want to sort the output of “ls” (Get-ChildItem) according to file size, date modified or alphabetically, try:

1. Sort according to file size:

ls | sort -property Length

2. Sort according to data modified:

ls | sort -property LastWriteTime

3. Sort according to Name:

ls | sort -property Name

To return a descending list, just add -descending at the back. This applies any stdout pipes, not just Get-ChildItem.

2 thoughts on “Easy sorting with Powershell”

  1. I’ve piped an object to select *, how to I get the output so that the properties of the object are sorted?

  2. When you pipe the properties, it should be a collection with property names like “Name, Date Modified” etc. Then just sort using sort -property [PropertyName]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>