Introduction to PowerShell Get-ChildItem
Get-ChildItem in PowerShell works similar to dir command in the windows command prompt. It can be used to retrieve the data from the specified location. In PowerShell subfolders, files or registry are called child items. If you want to retrieve the items from child containers then you need to use –Recurse parameter. Similar to dir /s in cmd.
Location can be a file system location, such as local directory, shared path directory, registry hive or certificate store. When you use Get-ChildItem for a system drive then it retrieves the directories, sub-directories, and files, but when you use it for directory then it retrieves the sub-directories and files underneath it. Its alias name is gci.
[[-Path] <string>] [[-Filter] <string>] [-Include <string>] [-Exclude <string>] [-Recurse] [-Depth <uint32>] [-Force] [-Name] [-Attributes <FlagsExpression[FileAttributes]>] [-FollowSymlink] [-Directory] [-File] [-Hidden] [-ReadOnly] [-System] [<CommonParameters>]
[[-Filter] <string>] -LiteralPath <string>
[-Include <string>] [-Exclude <string>] [-Recurse] [-Depth <uint32>] [-Force] [-Name] [-Attributes <FlagsExpression[FileAttributes]>] [-FollowSymlink] [-Directory] [-File] [-Hidden] [-ReadOnly] [-System] [<CommonParameters>]
Parameters of PowerShell Get-ChildItem
Following are the parameters of PowerShell Get-ChildItem explained below:
1) –Attributes <FileAttributes>: This parameter gets files and folders with specified attributes. When you use this parameter, you can specify a complex combination of attributes.
- To get System files that are encrypted.
Get-ChildItem -Attributes System+Encrypted
- To get non-system files (not Directory) which are encrypted or compressed.
Get-ChildItem -Attributes !Directory+!System+Encrypted, !Directory+!System+Compressed
You cannot use space between an operator and its attributes, but space is permitted before commas. The Attribute parameter supports the following attributes.
Following Operators can be used to combine attributes:
Following abbreviations are used for attributes:
- D: Directory
- H: Hidden
- R: Read-Only
- S: System
2) –Directory: When you use -Directory parameter, you will get only directories (folders) as a child item, this will exclude files from being displayed. To exclude directories, use -file attribute. Its alias is “d” or “ad”, depends on file system provider.
3) –File: The file attribute gives an output of only files under that container. To exclude files you need to use -Directory parameter. Its alias is “af”.
4) –Hidden: By default, Get-ChildItem displays non-hidden files and folders. If you want to display all files and folder including hidden ones then use -Force parameter. When you use -Hidden parameter then it will display only hidden files and folders. Its alias name is “h” or “ah”, depends on the file system provider.
5) –Readonly: It will display only read-only files and folders (directories). Its alias name is “ar”.
6) –System: This attribute will display only system files and folders. Its alias name is “as”.
7) –Force: This attribute will provide all files and folders including the hidden files and folders. By default, hidden files and folders are not included. We can also get hidden files and directories with -hidden parameters.
8) –UseTransaction: Includes the command in the active transaction. This parameter is valid when the current transaction is in progress. To learn more about transactions, check help in PowerShell about_Transactions.
9) –Depth: This parameter is used to control the recursion of directories. By default, Get-ChildItem provides you the parent files and folders and when you use recursion it provides all the subdirectories and their contents but when you use the Depth parameter, you can get the exact level of subdirectories and their content.
For example, When you provide Depth level 2, it gets you the contents from their first level subdirectories and second level subdirectories. When you use –Depth parameter –the Recurse parameter is not required. This parameter is introduced in Powershell 5.0.
10) –Exclude: This is a string parameter and it can exclude file, directory, extension, etc when specified after –Exclude from the path. You can use the wildcard characters, for example, *.txt, Test*.
11) –Include: This is a string parameter and when this parameter is used, it displays specific files and folders. For example, if *.txt is included then it will display only text files. You can include more than one choice. For example, *.txt, *.mp4 both can be included separated by comma (,).
12) –Filter: You can also filter the path with -filter parameter. The filter parameter is more efficient than include parameter because it retrieves the object while querying while other parameters retrieve after query. This parameter supports wildcard characters. The filesystem provider is the only PowerShell provider that supports the use of filters.
13) –Path: This parameter specifies the path of one or more locations. You can use wildcard characters with path and if the location not specified, it takes the current location as the default location.
14) –LiteralPath: This parameter specifies the path of one or more locations. Unlike -path parameter, you cannot specify wildcard characters here because this parameter can’t interpret characters as wildcards. If your path includes any escape characters then mark them under a single quote and PowerShell will consider it as a single path.
15) –Name: This parameter retrieves the only name of the items from Get-ChildItem output, not the path of the directory, mode, LastWriteTime, etc.
16) -CommonParameters: Below common parameters are used which are also called advance function’s parameters. Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningAction, WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
Properties and Operations Supported by Get-ChildItem
Following are the properties and operations supported by Get-ChildItem Explained below:
1. Properties of Directory
2. Properties of File
3. Methods of Directory
4. Methods of File
Examples of PowerShell Get-ChildItem
Below are some examples of PowerShell Get-ChildItem:
Example #1 – Parent Files and Folders
The below script will display parent files and folders.
Get-ChildItem -Path D:\Temp
Example #2 – Recurse Parameter
The below script will display sub files and folders contents but not hidden files.
Get-ChildItem -Path D:\Temp -Recurse
Example #3 – Depth Parameter
The below script will display sub files and folders contents up to 2 levels that are sub-folders and their sub-folders with their contents.
Get-ChildItem -Path D:\Temp -Recurse -Depth 2
Example #4 – Hidden Parameter
The below script will display only hidden files from the given path.
Get-ChildItem -Path D:\Temp -Recurse -Hidden
Example #5 – Include Parameter
Below script will include all files with extension *.xml
Get-ChildItem -Path D:\Temp -Recurse -Include *.xml
Example #6 – Exclude Parameter
The below script will exclude files and folders which start with S.
Get-ChildItem -Path D:\Temp -Exclude S*
Example #7 – Force Parameter
The below script will display sub-files and folder’s contents and hidden files as well.
Get-ChildItem -Path D:\Temp -Depth 1 -Force
Example #8 – Attribute Parameter
The below script will exclude directories and check for hidden files. Likewise, you can combine different attributes to get the desired results.
Get-ChildItem d:\Temp -Recurse -Attributes !Directory,!Directory+Hidden
Example #9 – Name Parameter
The below script will display only the names of the files and folders, excluding other parameters.
Get-ChildItem d:\Temp -Recurse -Attributes !Directory,!Directory+Hidden -Name
Example #10 – Registry Values
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
Example #11 – Certificates
To get all the certificates to use below command.
Get-ChildItem -Path Cert:\* -Recurse
This is a guide to PowerShell Get-ChildItem. Here we discuss the top 16 parameters, Operations and various examples of PowerShell Get-ChildItem. You may also look at the following articles to learn more –