SQL Server - Powershell Get-ChildItem with examples

16 December,2012 by Jack Vamvas

Question: How can list out all the items in a specified location using Powershell?

Answer: Use the  Get-ChildItem cmdlet . This cmdlet takes the DIR command and extends it into a flexible and powerful cmdlet.

This cmdlet will return the items in multiple locations.  An item can be a container – in that case it will return all the items in the container – known as child items.

A note on locations. Directories are typical locations , but other locations can be registries .

 

#An example of listing items in a directory
 Get-ChildItem -path "C:\laboratory"
 
#An example of listing items in a directory and -include 
 Get-ChildItem -path "C:\laboratory\*.*" -include *.txt
 
#An example of listing items in a directory and -exclude 
 Get-ChildItem -path "C:\laboratory\*.*" -exclude *.txt
 

#An example of listing the items, filtering name and length and using sort-object
Get-ChildItem -path "C:\laboratory"| select name,length | sort-object -property length

#An example to return the environment variables
Get-ChildItem env:

#An example of obtaining subkey information
Get-ChildItem HKLM:\SOFTWARE\Microsoft\

#An example to list all items with recurse
Get-ChildItem -path "C:\laboratory\" –Recurse

#Warning!!!!!!!!!!!!! This example DELETES all items at path 
Get-ChildItem -path "C:\laboratory\removeitem\" -Recurse | remove-item

 It is not always possible to use Powerrshell. An alternative to Powershell Get-ChildItem is Display all files in a directory with master.sys.xp_dirtree

Read More

Powershell Scripts

Powershell - run script on all sql servers

SQL Server - SQL Server Restart with Powershell 


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 - Powershell Get-ChildItem with examples


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