Aug 22 2008

Easy sorting with Powershell

Published by Lord TCT at 2:34 pm under Powershell

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

or just simply:

ls

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

2 responses so far

2 Responses to “Easy sorting with Powershell”

  1. Jane Barristeron 07 Dec 2009 at 2:56 pm

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

  2. Lord TCTon 19 Dec 2009 at 12:46 am

    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]

Comments RSS

Leave a Reply

Anti-spam challenge:
Sorry but you forced me to do this!