SQL Server - List Files in Size Order with Powershell

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

 Read Also on import-csv and free disk reporting

Use Powershell import-csv to enumerate list items from a CSV array

Free disk space on drives using Powershell

Powershell Scripts for DBA


Author: Jack Vamvas (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

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.

Working...

Post a comment on SQL Server - List Files in Size Order with Powershell


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer