Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: Get-Item vs Get-ChildItem

Tags:

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

like image 339
Schwitzd Avatar asked Jul 29 '16 16:07

Schwitzd


People also ask

What is Get-ChildItem in PowerShell?

Description. The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the Depth parameter to limit the number of levels to recurse.

What does get-Item do in PowerShell?

The Get-Item cmdlet gets the item at the specified location. It doesn't get the contents of the item at the location unless you use a wildcard character ( * ) to request all the contents of the item. This cmdlet is used by PowerShell providers to navigate through different types of data stores.

How do I Get-ChildItem to exclude folders?

To exclude directories, use the File parameter and omit the Directory parameter, or use the Attributes parameter. To get directories, use the Directory parameter, its "ad" alias, or the Directory attribute of the Attributes parameter.


2 Answers

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

like image 119
Chase Florell Avatar answered Sep 22 '22 22:09

Chase Florell


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 
like image 37
Steely Wing Avatar answered Sep 22 '22 22:09

Steely Wing