Reputation: 379
I would like understand the difference between Get-Item
and Get-ChildItem
.
If is possible with one example.
Upvotes: 33
Views: 25424
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
Upvotes: 29
Reputation: 17657
If the you have a folder like this
Folder
├─ File-A
└─ File-B
Get-ChildItem
will return the child of the item.Get-Item Folder
# Folder
Get-ChildItem Folder
# File-A File-B
Get-ChildItem
will return the item.Get-Item Folder\File-A
# File-A
Get-ChildItem Folder\File-A
# File-A
Upvotes: 9
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:
Upvotes: 7