SQL Server: Use of Dir | Get-ChildItem | LS in Powershell

The dir command allows you to see the available Files & Folders in a directory. The same command is available in Powershell to see all available Files and Folders in a Directory but the difference here is Powershell treats each file and folder as an Object. Reason being PS is based on .NET managed APIs, if .NET has certain Methods, Functions and Properties for a specified file it will also be treated as an Object.

In Powershell, there are two more cmdlet (command-let) equivalent to DIR viz.,

  1. Get-ChildItem  or gci (alias)
  2. ls (LS)  Yes, it has the support for Unix Command)

Get-ChildItem

Infact if we compare both the commands DIR \ Get-ChildItem in DOS and PS environment respecitvely, get-childitem is more powerful. you can add multiple filters while retreiving the result. Apart from this Force, recurse and exclude are few key switches to it which makes it my favourite.

Example 1: Get-ChildItem

This will  list down all the files and folders of the current location.

PS C:\Program Files> Get-ChildItem

Directory: C:\Program Files

Mode                LastWriteTime     Length Name

—-                ————-     —— —-

d—-        25-12-2010  14:14:54            Alwil Software

d—-        24-12-2010  23:56:48            Common Files

d—-        08-04-2010  04:55:11            CONEXANT

d—-        08-04-2010  05:39:38            DIFX

d—-        29-07-2009  12:53:50            DVD Maker

d—-        08-04-2010  04:50:33            Elantech

d—-        19-08-2010  09:02:40            Google

d—-        24-04-2011  12:12:53            Internet Explorer

d—-        08-04-2010  05:37:35            Lenovo

d—-        04-12-2010  21:25:38            MBlaze UI

d—-        03-06-2011  23:55:02            Microsoft Analysis Services

d—-        29-07-2009  12:53:50            Microsoft Games

d—-        08-04-2010  05:12:24            Microsoft Office

d—-        04-06-2011  00:00:16            Microsoft SQL Server

…………

Example 2:  Get-ChildItem m*

This command will list down all sub-directories where the name starts with M.

PS C:\Program Files> Get-ChildItem m*

Directory: C:\Program Files

Mode                LastWriteTime     Length Name

   

—-                ————-     —— —-

d—-        04-12-2010  21:25:38            MBlaze UI

d—-        03-06-2011  23:55:02            Microsoft Analysis Services

d—-        29-07-2009  12:53:50            Microsoft Games

d—-        08-04-2010  05:12:24            Microsoft Office

d—-        04-06-2011  00:00:16            Microsoft SQL Server

d—-        03-06-2011  23:46:11            Microsoft Sync Framework

d—-        03-06-2011  23:53:37            Microsoft.NET

d—-        14-07-2009  11:02:38            MSBuild

Example 3:  Get-ChildItem .\digicam-2-oct-2010\* -Include *2.jpg -exclude *4*.jpg

This command will pick the files from .\digicam-2-oct-2010\* location where the file name is ending with 2.JPG but it also exclude the files where a digit 4 is there in the entire number irrespective of the location. This brings in the power of Wildcard characters.

PS C:\> gci .\digicam-2-oct-2010\* -Include *2.jpg -exclude *4*.jpg

Directory: C:\digicam-2-oct-2010

Mode                LastWriteTime     Length Name

—-                ————-     —— —-

-a—        03-10-2010  00:07:36    4465311 DSC00192.JPG

-a—        03-10-2010  00:44:06    4503679 DSC00202.JPG

-a—        03-10-2010  15:15:38    4492951 DSC00212.JPG

-a—        03-10-2010  16:55:00    4502058 DSC00222.JPG

-a—        03-10-2010  17:03:12    4433692 DSC00232.JPG

-a—        03-10-2010  18:01:30    4373476 DSC00252.JPG

Moreover, if you want you can easily get the list of all files & folders in multiple sub-directories that too with a single command in one shot.

eNjoy

 

Regards

Sarabpreet Anand

Like us on FaceBook Follow us on Twitter | Join the fastest growing SQL Server group on FaceBook

Follow me on Twitter  |  Follow me on FaceBook

   

Leave a Reply

Your email address will not be published.