02 October,2012 by Tom Collins
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
Use Powershell import-csv to enumerate list items from a CSV array
Free disk space on drives using Powershell
This is only a preview. Your comment has not yet been posted.
As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.
Having trouble reading this image? View an alternate.
Posted by: |