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

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

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

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

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 directory 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 –Path parameter.

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

PowerShell Get ChildItem
PowerShell Get ChildItem

PowerShell Get-ChildItem Examples

Let’s understand how to use PowerShell to get childitem using a filter or get childitem in the directory.

Let’s practice!

PowerShell Get ChildItem Files only

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

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

In the above example, PowerShell get childitem gets all the files from the path specified by –Path parameter

The output of the above PowerShell GCI command, Mode a represent archive.

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 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 command to get results as above

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, PowerShell get-childitem command gets the file name only using Name property and location specified by -Path parameter.

You can use Get-ChildItem alias PowerShell GCI to get the file name from the directory as 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, using PowerShell GCI command get childitem file size only using the Length property.

PowerShell Get-ChildItem Recurse

If you want to get all files in directory and subdirectories, use the PowerShell Get-ChildItem Recurse parameter.

Recurse parameter search for all files in 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 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 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, we have specified multiple extensions like .zip and .txt, Include parameters, looking for files having multiple extensions, and returning results.

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

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

PowerShell Get Childitem Exclude Folders

Use -Exclude parameter to get childitem from file system exclude 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 exclude FTP-101 and FTP-102 from the path specified using -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 starts 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

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 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 to get childitem files only or files within directory 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 get 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