Home Β» PowerShell Β» PowerShell Get-ChildItem (gci,dir) Guide [With Examples]

PowerShell Get-ChildItem (gci,dir) Guide [With Examples]

PowerShell Get-ChildItem cmdlet gets the items and child items in or more specified location. PowerShell Get-ChildItem (GCI) is similar to the dir command in the Windows command prompt. Get-Childitem aliases are PowerShell gci, ls, and dir.

PowerShell gci - Get-ChildItem
PowerShell gci – Get-ChildItem

Get-ChildItem (GCI) gets items and if the item is a container, it will get child items available inside the container.

The location specified in PowerShell Get-ChildItem can be a file system directory, registry, or certificate store.

In this article, we will learn the PowerShell Get-ChildItem cmdlet to get child items from the directory, get childitem files only, and get childitem filter on condition.

Let’s understand the PowerShell Get-ChildItem cmdlet with examples.

PowerShell Get-ChildItem

Syntax

Get-ChildItem
   [[-Path] <string[]>]
   [[-Filter] <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Recurse]
   [-Depth <uint32>]
   [-Force]
   [-Name]
   [-Attributes <FlagsExpression[FileAttributes]>]
   [-FollowSymlink]
   [-Directory]
   [-File]
   [-Hidden]
   [-ReadOnly]
   [-System]
   [<CommonParameters>]

In the above syntax, use Recurse a parameter to get childitem in all child directories or containers, and a Depth parameter to specify a limit on a number of levels to recurse to get childitem.

How to Use PowerShell Get-ChildItem?

PowerShell Get-ChildItem gets the child items from the file system directory, registry, or certificate store.

Get-ChildItem -Path D:\PowerShell\

In the above command, Get-ChildItem gets child items from the path (D:\PowerShell) specified using the –Path parameter.

Using the Get-ChildItem with the location specified using the Path parameter, it displays files, and directories with their LastWriteTime, Length (file size), and Name on the PowerShell console

PowerShell Get ChildItem
PowerShell Get ChildItem

PowerShell Get ChildItem Files only

Use the PowerShell Get-ChildItem cmdlet with the –File parameter to filter and get childitem files only.

PS C:\> Get-ChildItem -Path D:\PowerShell\ -File 

In the above PowerShell script, the Get-Childitem command uses the Path parameter to specify the directory path from where you want to retrieve all files, in this case,”D:\PowerShell\β€œ. The -File parameter is used with the command to get all the files from the directory.

The output of the above PowerShell GCI command displays the list of files from the directory that includes File Mode, LastWriteTime, Length, and Name. The Length attribute represents the size of the file in bytes.

PS C:\> Get-ChildItem -Path D:\PowerShell\ -File   
                Directory: D:\PowerShell


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       04-10-2020     14:40             51 ActiveDirectory.csv
-a----       24-10-2020     19:15             29 complist.txt
-a----       24-10-2020     20:08            118 comppingstatus.csv

You can also use the PowerShell Get-ChildItem cmdlet alias GCI to get files from a directory.

PowerShell get childitem files only
PowerShell get childitem files only

Use the following PowerShell GCI command to get all files only from the folder.

PS C:\> gci -Path D:\PowerShell\ -File 

In the above script, PowerShell gci gets files only from the specified directory.

Cool Tip: How to get file creation date in PowerShell!

PowerShell Get-ChildItem File Name

PS C:\> Get-ChildItem -Path D:\PowerShell -File | Select Name    

In the above example, the PowerShell get-childitem command gets the file name only using Name property from the location specified by -Path parameter.

You can use the Get-ChildItem alias PowerShell GCI to get the file name from the directory below.

Gci -Path D:\PowerShell -File | Select Name

PowerShell Get ChildItem File Size only

Get-ChildItem -Path D:\PowerShell -File | Select Length 

In the above example, the PowerShell Get-ChildItem command uses the Path parameter to specify the location to retrieve all files and pipes all the files to the Select command. The Select command uses the Length property to get childitem file size.

PowerShell Get-ChildItem Recurse

To get all files in the directory and subdirectories, use the PowerShell Get-ChildItem command with the Recurse parameter.

The Recurse parameter searches for all files in the directory and subdirectories.

Get-ChildItem -Path D:\PowerShell\ -Recurse -Force -File

In the above PowerShell GCI example, the get-childitem Recurse parameter along with -Force gets all files in the directory and subdirectories.

Cool Tip: Learn how to get aduser using userprincipalname!

PowerShell Get Childitem Include Multiple Extensions

Let’s consider an example to get files having extension .txt and .zip, using the PowerShell Get-ChildItem cmdlet –Include parameter, it gets input as multiple extensions and returns files.

Get-ChildItem -Path D:\PowerShell\* -Include *.zip,*.txt  

In the above PowerShell GCI command, the Get-ChildItem cmdlet uses the INclude parameter to specify multiple extensions like .zip and .txt, etc.. and searches for files having these extensions and returns results.

Note: Use * after the specified location else it will return an empty result.

Cool Tip: How to add a new line to string or variable in PowerShell!

PowerShell Get Childitem Exclude Folders

The Get-ChildItem cmdlet uses the -Exclude parameter to get childitem from the file system and excludes specified folders. You can specify multiple folders to exclude in the command.

Get-ChildItem -Path D:\PowerShell\* -Directory -Name -Exclude FTP-101,FTP-102   

Above PowerShell get childitem command returns all folders excluding FTP-101 and FTP-102 from the path specified using the -Exclude parameter.

Cool Tip: Do you know about using PSScriptRoot automatic variable in PowerShell!

How to get Registry keys using Get-ChildItem in PowerShell

Get-ChildItem uses the Path parameter to specify the registry key.

Get-ChildItem -Path HKLM:\Software

It shows all the content of the registry key HKLM:\Software hive.

To exclude any subkeys that start with some character, you can use the below command

Get-ChildItem -Path HKLM:\Software -Exclude D*

The Exclude parameter in the above command excludes subkeys that start/ with D.

The output of the above command to get registry keys from the hive is given below

Get registry keys from hive using gc
Get registry keys from hive using GCI

Get all certificates using Get-ChildItem

Get-ChildItem -Path Cert:\* -Recurse

In the above Get-ChildItem example, it gets all certificates available in the Cert:\.

Get Hidden files and Directory in PowerShell

Get-ChildItem -Path D:\LogTest\FTP-01\ -Attributes !Directory,!Directory+Hidden

In the above PowerShell script, the Get-ChildItem cmdlet uses the Path parameter to specify the location from where it should get all the files and Attributes parameter to specify !Directory and !Directory+Hidden value to retrieve hidden files and directories.

This command gets hidden files in the directory specified by the path parameter.

You can get the hidden directory from the specified directory using the below command.

Get-ChildItem -Path D:\LogTest\FTP-01\ -Attributes Directory+Hidden

This command gets a hidden directory specified in a given directory using Attributes Directory + Hidden as given below:

Get hidden files or directory in PowerShell
Get hidden files or directories in PowerShell

Get Files Owned by Specified User

Using Get-ChildItem, you can get files owned by users using the Get-Acl cmdlet.

Get-childitem -Path D:\LogTest\FTP-02\ -recurse | get-acl | where {$_.Owner -match "ShellAdmin"}  

This command retrieved files owned by user ShellAdmin.

The output of the above file owned by the specified user is as below.

Get files owned by the user
Get files owned by the user

Conclusion

I hope the above article on Get-ChildItem (GCI) cmdlet and its examples is to get childitem files only or files within directories and subdirectories.

You can use the PowerShell Get-ChildItem cmdlet to get folders only or folder properties.

Use Measure-Object to perform a calculation on file object to count files in folders only or subfolders.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment