Get-ChildItem | Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Get-ChildItem.

When to use Get-ChildItem?

The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the -Recurse parameter to get items in all child containers and use the -Depth parameter to limit the number of levels to recurse.

Get-ChildItem does not display empty directories. When a Get-ChildItem command includes the -Depth or -Recurse parameters, empty directories are not included in the output.

Locations are exposed to Get-ChildItem by PowerShell providers. A location can be a file system directory, registry hive, or a certificate store.

What version of PowerShell am I using for this blog?

Get the PowerShell Version from your machine:

$PSVersionTable

This command shows you the PowerShell version information on your machine.

How to use Get-ChildItem?

Get child items from a file system directory: 

Get-ChildItem -Path C:\PShellTest

This example gets the child items from a file system directory. The filenames and subdirectory names are displayed. For empty locations, the command does not return any output and returns to the PowerShell prompt.

The Get-ChildItem cmdlet uses the -Path parameter to specify the directory C:\PShellTest. Get-ChildItem displays the files and directories in the PowerShell console.

By default, Get-ChildItem lists the mode (Attributes), LastWriteTime, file size (Length), and the Name of the item. The letters in the Mode property can be interpreted as follows:

  • l (link)
  • d (directory)
  • a (archive)
  • r (read-only)
  • h (hidden)
  • s (system)

Get child item names in a directory:

Get-ChildItem -Path C:\PShellTest -Name

The Get-ChildItem cmdlet uses the -Path parameter to specify the directory C:\PShellTest.

The -Name parameter returns only the file or directory names from the specified path.

Get child items in the current directory and 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 asterisk (*) wildcard to specify all files with the filename extension .txt.

The -Recurse parameter searches the Path directory its subdirectories, as shown in the Directory: headings. The -Force parameter displays hidden files that have a mode of h.

Get child items using the Include parameter:

Get-ChildItem -Path C:\Windows\System32\* -Include *.txt

The Get-ChildItem cmdlet uses the -Path parameter to specify the directory C:\Windows\System32. The -Path parameter includes a trailing asterisk (*) wildcard to specify the directory’s contents.

The -Include parameter uses an asterisk (*) wildcard to specify all files with the file name extension .txt.

When the -Include parameter is used, the -Path parameter needs a trailing asterisk (*) wildcard to specify the directory’s contents.

  • If the -Recurse parameter is added to the command, the trailing asterisk (*) in the -Path parameter is optional. The -Recurse parameter gets items from the Path directory and its subdirectories.
  • If a trailing asterisk (*) is not included in the -Path parameter, the command does not return any output and returns to the PowerShell prompt.

Get child items using the Exclude parameter:

Get-ChildItem -Path C:\Windows\System32\* -Include *.txt -Exclude C*

The Get-ChildItem cmdlet uses the -Path parameter to specify the directory C:\Windows\System32. The -Path parameter includes a trailing asterisk (*) wildcard to specify the directory’s contents.

The -Include parameter uses an asterisk (*) wildcard to specify all files with the file name extension .txt.

The -Exclude parameter uses the asterisk (*) wildcard to specify any files or directories that begin with C or c are excluded from the output.

When the -Include parameter is used, the -Path parameter needs a trailing asterisk (*) wildcard to specify the directory’s contents.

  • If the -Recurse parameter is added to the command, the trailing asterisk (*) in the -Path parameter is optional. The -Recurse parameter gets items from the Path directory and its subdirectories.
  • If a trailing asterisk (*) is not included in the -Path parameter, the command does not return any output and returns to the PowerShell prompt.

Get items using the Depth parameter:

Get-ChildItem -Path C:\Azure -Depth 1

The Get-ChildItem cmdlet uses the -Path parameter to specify C:\Azure. The -Depth parameter specifies one level of recursion.

Learn last week’s command: Copy-Item.

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.