Powershell:Get-Item vs Get-ChildItem

G4r*_*4rp 12 powershell

我想明白之间的差别Get-ItemGet-ChildItem.如果可以用一个例子.

Cha*_*ell 19

Get-Item
获取指定位置的项目.

Get-Item .\foo
# returns the item foo
Run Code Online (Sandbox Code Playgroud)

Get-ChildItem
获取一个或多个指定位置中的项和子项.

Get-ChildItem .\foo
# returns all of the children within foo
Run Code Online (Sandbox Code Playgroud)

注意:Get-ChildItem也可以递归到子目录中

Get-ChildIten .\foo -Recurse
# returns all of the children within foo AND the children of the children
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在此输入图像描述

在此输入图像描述


小智 7

获取当前目录

Get-Item .
Run Code Online (Sandbox Code Playgroud)

获取当前目录下的所有项目

# use Get-Item
Get-Item *
# or use Get-ChildItem
Get-ChildItem
Run Code Online (Sandbox Code Playgroud)

获取所有txt文件

# use Get-Item
Get-Item *.txt
# or use Get-ChildItem
Get-ChildItem *.txt
Run Code Online (Sandbox Code Playgroud)

参考:

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


Ste*_*ing 7

如果你有这样的文件夹

Folder
?? File-A
?? File-B
Run Code Online (Sandbox Code Playgroud)
  • 如果项目是文件夹,Get-ChildItem将返回项目的子项。
Folder
?? File-A
?? File-B
Run Code Online (Sandbox Code Playgroud)
  • 如果项目不是文件夹,Get-ChildItem将返回项目。
Get-Item Folder
# Folder

Get-ChildItem Folder
# File-A File-B
Run Code Online (Sandbox Code Playgroud)