PowerShell: The Difference Between Get-Item vs. Get-ChildItem


The Get-Item and Get-ChildItem cmdlets in PowerShell can both be used to retrieve specific items from specific locations on your computer, but they behave in slightly different ways.

If you want to get a specific file, the Get-Item and Get-ChildItem cmdlets will return the same results.

If you want to get a specific folder, the Get-Item cmdlet will only return the folder itself while the Get-ChildItem cmdlet will return all of the contents inside the folder.

The following examples illustrate this difference in practice.

Example 1: Use Get-Item and Get-ChildItem to Retrieve File

Suppose that we would like to retrieve a file named cities.txt in our current directory.

The following screenshot shows how to use both the Get-Item and Get-ChildItem cmdlets to retrieve this file:

Notice that both the Get-Item and Get-ChildItem cmdlets return the file that we specified.

In this scenario, the two cmdlets produced the same results.

Example 2: Use Get-Item and Get-ChildItem to Retrieve Folder

Suppose that we would like to retrieve a folder named data1 in our current directory.

The following screenshot shows how to use both the Get-Item and Get-ChildItem cmdlets to retrieve this folder:

PowerShell Get-Item vs. Get-ChildItem

Notice that the Get-Item cmdlet only returns the folder itself.

However, the Get-ChildItem cmdlet returns the folder along with all of the files inside of it.

It’s also worth noting that you can use the -Recurse parameter with the Get-ChildItem cmdlet to get all of the files inside any subfolders of a specific folder:

Get-ChildItem data1 -Recurse

The Get-Item cmdlet does not have this option.

In summary:

  • The Get-Item cmdlet is best suited for retrieving a single file.
  • The Get-ChildItem cmdlet is best suited for retrieving multiple files and /or subfolders inside of a folder.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Use Get-ChildItem with Filter
PowerShell: Use Get-ChildItem to Return Folders Only
PowerShell: How to Use Get-ChildItem to Get Full Path of Files

Leave a Reply