Get-ChildItem Powershell Cmdlet
Get-ChildItem obtains objects from one or more locations specified. If the item is a container, it receives the child items contained within the container. The Recurse parameter can be used to get items from all child containers, while the Depth parameter can be used to limit how many levels to recurse to.
Empty folders are not displayed by Get-ChildItem. When the Depth or Recurse arguments are used in a Get-ChildItem command, empty directories are not included in the result.
PowerShell providers expose locations to this cmdlet. A file system directory, registry hive, or certificate store can all be used as a location.
Below command-let will fetch all the files and folders from C drive.
Table Of Contents
Retrieving Child Items from a File System Directory
The child items in this example are obtained from a file system directory. The names of the files and subdirectories are displayed. For empty locations, the command returns to the PowerShell prompt without producing any output.
The Path option of the Get-ChildItem cmdlet is used to indicate the directory C:\Program Files. In the PowerShell console, Get-ChildItem displays the files and directories.
Get-ChildItem displays the item’s mode (Attributes), LastWriteTime, file size (Length), and Name by default. The letters in the Mode property have the following meaning.
- l (link)
- d (directory)
- a (archive)
- r (read-only)
- h (hidden)
- s (system)
Get-ChildItem -Path C:\”Program files”
Getting the Child Item Names in a Directory
This Name parameter just shows the names of directory objects. The Path option of the Get-ChildItem cmdlet is used to indicate the directory C: \Program Files. Only the file or directory names from the supplied path are returned by the Name parameter.
Get-ChildItem -Path C:\"Program Files" -Name
Getting Child Items in Current Directory and Subdirectories
We can show the contents of all .txt files in the current directory and its subdirectories.
The Path option of the Get-ChildItem cmdlet is used to specify C:Test Folder\*.txt. To specify all files with the filename extension.txt, Path employs the asterisk (*) wildcard. As seen in the Directory: headers, the Recurse parameter explores the Path directory and its subdirectories. The Force parameter shows hidden files with a mode of h, such as hiddenfile.txt.
Get-ChildItem -Path C:\"Test Folder"\*.txt -Recurse -Force
Getting Child Items Using the Include Parameter
The Include option is used in this example to find specific objects in the directory supplied by the Path parameter. The Path option of the Get-ChildItem cmdlet is used to indicate the directory C:\Test Folder.
To provide the directory’s contents, the Path option contains a trailing asterisk (*) wildcard. The Include argument specifies all files with the extension *.log using an asterisk (*) wildcard.
The Path parameter requires a trailing asterisk (*) wildcard when they Include parameter is used to indicate the directory’s contents.
If the Path option does not include a trailing asterisk (*), the command returns to the PowerShell prompt with no output.
The below command-let does not bring any results as there is no * included in path.
Get-ChildItem -Path C:\”Test Folder”\ -Include *.log
The trailing asterisk (*) in the Path parameter is unnecessary if the Recurse parameter is supplied to the command. The Path directory and its subdirectories are searched using the Recurse parameter. -Path
Get-ChildItem -Path C:\Test Folder -Recurse -Include *.log
The below command-let with * in path will bring the desired results with include parameter.
Get-ChildItem -Path C:\”Test Folder”\* -Include *.log
Getting Child Items Using the Exclude Parameter
The exclude parameter is used to show the content of a directory mentioned in path parameter where you want to exclude some file names.
Same as include parameter, exclude also uses an asterisk (*) to fetch the results. And also, an asterisk is a requirement in Path parameter, then the correct output you will get.
Get-ChildItem -Path C:\”Test Folder”\* -exclude S*
We will run the simple command-let first to see the total content and then will filter using exclude parameter. As shown below.
Get-ChildItem -Path C:\”Test Folder”\ -Recurse
In the below screenshot, you can see that we used the -Recurse parameter to see the sub-directories content, including the two files starting with “setup*”.
Get-ChildItem -Path C:\”Test Folder”\* -exclude S*
In the below screenshot, the results are filtered by exclude parameter
Getting the Registry Values from a Registry Node
We will use the Path parameter for registry nodes to get the registry information, we will use the short form of “HKEY_LOCAL_MACHINE\SOFTWARE” as “HKLM\SOFTWARE”, which will display the top level of registry keys in PowerShell console.
Get-ChildItem -Path HKLM:\SOFTWARE
The above command-let shows the full contents of Software registry key, we will use the Exclude parameter to filter some of the registry keys we don’t want. We are filtering the keys starting from R and C characters, which will filter “Classes, Client and Register Applications” registries.
Get-ChildItem -Path HKLM:\SOFTWARE -Exclude R*,C*
You can also retrieve various registry values as displayed below.
Get-ChildItem -Path Registry::HKEY_LOCAL_MACHINE
Get-ChildItem -Path Registry::HKEY_CLASSES_ROOT
Get-ChildItem -Path Registry::HKEY_CURRENT_CONFIG
Get-ChildItem -Path Registry::HKEY_CURRENT_USER
Get-ChildItem -Path Registry::HKEY_USERS
Get-ChildItem -Path Registry::HKEY_PERFORMANCE_DATA
Getting all Certificates with Code-signing Authority
Below example retrieves each certificate with code-signing authority from the PowerShell Cert: folder.
The Path option of the Get-ChildItem cmdlet is used to define the Cert: provider. The Recurse parameter looks through the Path directory and its subdirectories. Only certificates with code-signing authority are returned by the CodeSigningCert argument.
Get-ChildItem -Path Cert:\* -Recurse -CodeSigningCert
Getting Items Using the Depth Parameter
The objects in a directory and its subdirectories are displayed in this example. The number of subdirectory levels to include in the recursion is determined by the Depth argument. The output is not affected by empty folders.
The Path option of the Get-ChildItem cmdlet is used to define C:Drivers. Two levels of recursion are specified by the Depth argument. The contents of the directory supplied by the Path parameter, as well as the two levels of subdirectories, are displayed by Get-ChildItem.
Get-ChildItem -Path C:\Drivers -Depth 2
Searching Items with Specific Attributes
Gets files and folders with the attributes specified. This option accepts all characteristics and allows you to specify complex attribute combinations.
For example, to get hidden files (not directories) from the specified directory, type:
Get-ChildItem -Path C:\”Test Folder”\ -Attributes !Directory+ Hidden,!Directory+Hidden
Use the Attributes option to identify files and directories with widely used attributes. The parameters Directory, File, Hidden, ReadOnly, and System are used widely.
The Attributes parameter supports the following properties:
Use the following operators to combine attributes:
- ! (NOT)
- + (AND)
- , (OR)
Between an operator and its attribute, there should be no spaces. After commas, spaces are allowed.
Use the abbreviations below for common properties:
- D (Directory)
- H (Hidden)
- R (Read-only)
- S (System)
Search Items Using Filter
To qualify the Path parameter, specify a filter. Filters are only supported by the File System provider, which is the only PowerShell provider installed. Other parameters are less efficient than filters. Rather of having PowerShell filter the objects after they’re retrieved, the provider filters them when the cmdlet gets them. To enumerate files, the filter string is supplied to the.NET API. Only the wildcards * and? are supported by the API.
With below command-let, you can query all the word files in the specified directory.
Get-ChildItem C:\"Test Folder"\* -filter *.docx
Get a List of Directories
Directory parameter can be used to get a list of directories, you can also use Attributes parameter with the directory property to achieve the same goal.
With Directory, you can use the Recurse parameter to see the sub-directories.
Below command-let will provide results in the form of 3 columns (Mode, LastWriteTime and Name) for top level directories.
Get-ChildItem -Path C:\ -Directory
Using the Recurse parameter you can list the sub-directories too with the above format.
Get-ChildItem -Path C:\GroupID10SR1\ -Directory -Recurse
If you want to see only the directories and sub-directories with their hierarchy, use the Name parameter with the Directory parameter.
Get-ChildItem -Path C:\GroupID10SR1\ -Directory -Name -Recurse
Get the List of Files Only
Use the File option to receive a list of files. With File, you can use the Recurse argument.
Below simple command-let will only show you the files from the specified path.
Get-ChildItem -Path C:\ -File
But if you want to see the files from specified path and its sub-directories, then use the Recurse parameter to get the below output.
Get-ChildItem -Path C:\GroupID10SR1\ -File -Recurse