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