02 October,2012 by Jack Vamvas
Question: How do I list all files in a directory and order the files by Size in ascending order, using Powershell?
Answer: It is possible to list the files in a directory using the Powershell cmdlets – Get-ChildItem and Sort-Order.
The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers.
The Sort-Object cmdlet sorts objects in ascending or descending order based on the values of properties of the object. In the example below, I’ve select a single property i.e length. There are many other properties to sort on – such as Descending, Case sensitive and you can also create your own sort expressions
#An example of listing items in a directory Get-ChildItem -path "C:\projects\testing\laboratory" #An example of listing the items, filtering name and length and using sort-object Get-ChildItem -path "C:\projects\testing\laboratory"| select name,length | sort-object -property length