Share this post: This is part of an ongoing blog series by Adam Gordon. Adam will guide you through a PowerShell command each week, explaining when and how to use them. Adam will be covering Get-ChildItem this week.
When is it appropriate to use Get-ChildItem
The Get-ChildItem cmdlet allows you to place the items in a specified location. If the item is a container it will get the items inside the container. These are known as child items. To get items in all child containers, you can use the parameter -Recurse and the parameter -Depth to limit the levels to which you can recurse.
Get-ChildItem doesn’t display empty directories. Empty directories are not displayed when Get-ChildItem commands include the -Depth and -Recurse parameters.
PowerShell providers expose locations to Get-ChildItem. A location can be a registry hive, file system directory, or certificate store.
What version of PowerShell should I use for this blog?
Get the PowerShell Version for your machine
$PSVersionTable
This command displays the PowerShell version information for your machine.
How to use GetChildItem
You can get child items from a directory file system:
Get-ChildItem -Path C:\PShellTest
This example shows the child items of a file system directory. The filenames and names of subdirectories are displayed. The command returns no output if the location is empty and will return to the PowerShell prompt.
The Get-ChildItem cmdlet uses a -Path parameter in order to specify the directory C.PShellTest. Get-ChildItem displays files and directories from the PowerShell console.
Get-ChildItem defaults to listing the mode (Attributes), LastWriteTime and file size (Length) as well as the name of the item. The Mode property can be read as follows:
l (link).
d (directory).
A (archive).
r (read only)
h (hidden).
s (system).
List child item names in a directory
Get-ChildItem -Path C:\PShellTest -Name
The Get-ChildItem cmdlet uses a -Path parameter in order to specify the directory C.PShellTest.
The -Name parameter returns only file or directory names from the specified path.
Make sure to include child items in the current directory as well as subdirectories.
Get-ChildItem -Path C:\Windows\System32\DriverStore\*.txt -Recurse -Force
The Get-ChildItem cmdlet uses the -Path parameter to specify the C:\Windows\System32\DriverStore path. Path uses the wildcard asterisk (*), to specify all files that have the filename extension of.txt.
The -Recurse parameter searches Path directory and its subdirectories as shown in Directory: headings. Hidden files with a mode of “h” are displayed by the -Force parameter.
Use the Include parameter to get child items
Get-ChildItem -Path C:\Windows\System32\* -Include *.txt
The -Path parameter is used by the Get-ChildItem cmdlet to specify the directory C.WindowsSystem32. To specify the directory’s contents, the -Path parameter uses a trailing asterisk (“*”) wildcard.
To specify all files with the file extension.txt, the -Include parameter uses an asteris (*) wildcard.
The -Include parameter must be used. To specify the directory’s contents, the -Path parameter must contain a trailing asterisk(*) wildcard.
The optional trailing asterisk * in the Path parameter will be ignored if the -Recurse parameter has been added to the command. The -Recurse parameter retrieves items from the Path directory as well as its subdirectories.
If the -Path parameter does not include a trailing asterisk, (*), the command returns no output and the PowerShell prompt.
Use the Exclude parameter to get child items
Get-ChildItem -Path C:\Windows\System32\* -Include *.txt -Exclude C*
The -Path parameter is used by the Get-ChildItem cmdlet to specify the directory C.WindowsSystem32. To specify the directory’s contents, the -Path parameter uses a trailing asterisk (“*”) wildcard.
To specify all files with the file extension.txt, the -Include parameter uses an asteris (*) wildcard.
The -Exclude parameter uses an asterisk (*), wildcard to indicate files or directories that start with C or C are excluded from the output.
The -Include parameter must be used. To specify the directory’s contents, the -Path parameter must contain a trailing asterisk(*) wildcard.
The optional trailing asterisk * in the Path parameter will be ignored if the -Recurse parameter has been added to the command. The -Recurse parameter retrieves items from the Path directory as well as its subdirectories.
If the -Path parameter does not include a trailing asterisk, (*), the command returns no output and the PowerShell prompt.
Use the Depth parameter to get items
Get-ChildItem -Path C:\Azure -Depth 1
The Get-ChildItem cmdlet uses -Path to specify C:Azure. The -Depth parameter specifies a level of recursion.
Copy-Item: Last week’s command
Do you need PowerShell training? ITProTV offers PowerShell online IT training courses.