Get-ChildItem Cmdlet To Loop Files and Results
Get-ChildItem is very useful cmdlet provided by Powershell. This cmdlet has two main usages. One is if the directory is provided as a parameter the given directory files and folders will be enumerated like a loop. Second is if a list or container is provided all the child will we enumerated. Let’s start action to learn details.
Syntax is like below.
Get-ChildItem OPTIONS PATH
- OPTIONS used for different behaviors
- PATH is used to specify path without any option.
We can print help information about
Get-ChildItem cmdlet like below. We will use
Get-Help cmdlet to print help information about
PS> Get-Help Get-ChildItem
List All Files And Folders In Current Directory
We will start with a simple example where we do not provide any option to the
Get-ChildItem cmdlet. This will print all files and folders in the current working directory.
We can see that the following information is provided.
- Mode like read, write execute
- Length which specifies the size
Shown Only Given Extension
In this example, we will only show files with the specific extension. We can provide the extension after the cmdlet. In this case, we will list System file extension which can be specified as
*.sys like below.
PS> Get-ChildItem *.sys
We can also use
-Path option we can also provide the extension like below.
PS> Get-ChildItem -Path *.sys
The default behavior of the
Get-ChildItem is running on the current working directory. If we want to run this cmdlet in the different path we can specify the path with the
-Path option. In this example, we will run on the
Windows directory in the
C: partition like below. Using double quotes will make the path specification more stable because spaces in the path may create problems.
PS> Get-ChildItem -Path "c:\Windows"
The default behavior is listing current working directory or path to list. If we want to list the subfiles and subfolders we need to make the listing in a recursive manner. We can enable recursion with the
-Recurse option like below.
PS> Get-ChildItem -Recurse
We can see that there are some errors printed in red because we do not have access to this path.
Specify Recursion Depth
By specifying recursion we will let the
Get-ChildItem command list all subfiles and subfolders. This means recursive list will go to the 100 level if it exists. This may create some performance or output problems. We can limit the depth of recursive listing with the
-Depth option. We will provide the level as a number like 5. In this example, we will only list depth with 1 like below.
PS> Get-ChildItem -Recurse -Depth 2
Exclude Some Files
While listing we may need to exclude some files according to their names or extensions. We will use
-Exclude option and provide the file or folder name. In this example, we will exclude according to their extension where will exclude files with
PS> Get-ChildItem -Exclude "*.sys"
Include Files and Extension
While excluding some files according to their names and extensions we can also include some file names and extensions with the
-Include option. In this example we will exclude
*.tmp extension but include files with name
check even their extension is
PS> Get-ChildItem -Exclude "*.tmp" -Include "*check*"
List All Registry Key
Get-ChildItem cmdlet can work with the registry by using
HKLM to the
-Path option. We can list all registry keys like below. We will search in
Software hive in this example.
PS> Get-ChildItem -Path HKLM:\Software
List All Certificates
Another useful feature of the
Get-ChildItem cmdlet is listing currently installed and used certificates. We will use
-Path with the
Cert parameter like below.
PS> Get-ChildItem -Path Cert:\
List Only Name of The Files and Folders
The default behavior of the
Get-ChildItem cmdlet is listing files names with other attributes like size, mode etc. If we just want to list the names of the files we need to provide
-Name option like below.
PS> Get-ChildItem -Name