Schwitzd
Schwitzd

Reputation: 379

Powershell: Get-Item vs Get-ChildItem

I would like understand the difference between Get-Item and Get-ChildItem. If is possible with one example.

Upvotes: 33

Views: 25424

Answers (3)

Chase Florell
Chase Florell

Reputation: 47427

Get-Item
Gets the item at the specified location.

Get-Item .\foo
# returns the item foo

Get-ChildItem
Gets the items and child items in one or more specified locations.

Get-ChildItem .\foo
# returns all of the children within foo

note: Get-ChildItem can also recurse into the child directories

Get-ChildItem .\foo -Recurse
# returns all of the children within foo AND the children of the children

enter image description here

enter image description here

enter image description here

Upvotes: 29

Steely Wing
Steely Wing

Reputation: 17657

If the you have a folder like this

Folder
├─ File-A
└─ File-B
  • If the item is a folder, Get-ChildItem will return the child of the item.
Get-Item Folder
# Folder

Get-ChildItem Folder
# File-A File-B
  • If the item is not a folder, Get-ChildItem will return the item.
Get-Item Folder\File-A
# File-A

Get-ChildItem Folder\File-A
# File-A

Upvotes: 9

ysmiles
ysmiles

Reputation: 71

Get the current directory

Get-Item .

Get all the items in the current directory

# use Get-Item
Get-Item *
# or use Get-ChildItem
Get-ChildItem

Get all txt files

# use Get-Item
Get-Item *.txt
# or use Get-ChildItem
Get-ChildItem *.txt

Reference:

https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Management/Get-Item?view=powershell-5.1

https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Management/Get-ChildItem?view=powershell-5.1

Upvotes: 7

Related Questions