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

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.

PowerShell Get-ChildItem cmdlet gets the items and child items in or more specified location. PowerShell Get-ChildItem (gci) is similar to dir command in windows command prompt.

Get-ChildItem (gci) gets items and if the item is 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 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, using Recurse parameter to get childitem in all child directory or containers and Depth parameter to specify limit on number of levels to recurse to get childitem.

How to Use PowerShell Get-ChildItem?

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

Get-ChildItem -Path D:\PowerShell\

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

Get-ChildItem cmdlet on run, display files, directories with their LastWriteTime, Length (file size), Name on PowerShell console as below

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 directory.

PowerShell Get ChildItem Files only

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

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

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

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

    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 directory. Use below 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 get 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 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 file in directory and subdirectories.

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

In the above PowerShell gci example, 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 get input as multiple extensions and return files.

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

In the above PowerShell gci command, we have specified multiple extension as .zip and .txt, Include parameters, looks for file having multiple extensions and returns result.

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

Cool Tip: How to add 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 command

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

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

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

Conclusion

I hope above article on PowerShell Get-ChildItem (gci) cmdlet and its examples to get childitem file only or files within directory and subdirectories. You can use the Get-ChildItem cmdlet to get folders only or folder properties.

Use Measure-Object to perform 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 ShellGeek home page.

Leave a Comment