用于 FileSystem 的 Get-ChildItem

应用到: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

获取文件系统驱动器中的文件和文件夹。

语法

Get-ChildItem [-Attributes <FileAttributes]>] [-Directory] [-File] [-Force] [-Hidden] [-ReadOnly] [-System] [-UseTransaction] [<CommonParameters>]

描述

在文件系统驱动器中,Get-ChildItem cmdlet 将获取目录、子目录和文件。在文件系统目录中,它将获取子目录和文件。

默认情况下,Get-ChildItem 将获取非隐藏项,但你可以使用 Directory、File、Hidden、ReadOnly 和 System 参数以仅获取具有这些属性的项。若要创建复杂的属性搜索,请使用 Attributes 参数。如果使用这些参数,则 Get-ChildItem 仅获取符合所有搜索条件的项,就像这些参数已通过 AND 运算符连接一样。

注意:此自定义 cmdlet 帮助文件介绍了 Get-ChildItem cmdlet 在文件系统驱动器中的工作方式。有关所有驱动器中的 Get-ChildItem cmdlet 的信息,请键入“Get-HelpGet-ChildItem -Path $null”或参阅 Get-ChildItem,网址为 https://go.microsoft.com/fwlink/?LinkID=113308。

参数

-Attributes <FileAttributes]>

获取具有指定属性的文件和文件夹。此参数支持所有属性,并且允许你指定复杂的属性组合。

例如,若要获取加密或压缩的非系统文件(而不是目录),请键入:

Get-ChildItem -Attributes !Directory+!System+Encrypted, !Directory+!System+Compressed

若要查找具有常用属性的文件和文件夹,可以使用 Attributes 参数,或 Directory、File、Hidden、ReadOnly 和 System 开关参数。

Attributes 参数支持以下属性:Archive、Compressed、Device、Directory、Encrypted、Hidden、Normal、NotContentIndexed、Offline、ReadOnly、ReparsePoint、SparseFile、System 和 Temporary。有关这些属性的说明,请参阅 FileAttributes 枚举,网址为 https://go.microsoft.com/fwlink/?LinkId=201508。

使用以下运算符合并属性。

! NOT

+ AND

, OR

运算符与其属性之间不允许有空格。但是,在逗号之前允许有空格。

对于常用属性,可以使用下列缩写:

D Directory

H Hidden

R Read-only

S System

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-Directory

获取目录(文件夹)。

若要仅获取目录,请使用 Directory 参数并省略 File 参数。若要排除目录,请使用 File 参数并省略 Directory 参数,或者使用 Attributes 参数。

若要获取目录,请使用 Directory 参数、其“ad”别名或 Attributes 参数的 Directory 属性。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-File

获取文件。

若要仅获取文件,请使用 File 参数并省略 Directory 参数。若要排除文件,请使用 Directory 参数并省略 File 参数,或者使用 Attributes 参数。

若要获取文件,请使用 File 参数、其“af”别名或 Attributes 参数的 File 值。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-Hidden

仅获取隐藏的文件和目录(文件夹)。默认情况下,Get-ChildItem 仅获取非隐藏项,但可以使用 Force 参数以将隐藏项包含在结果中。

若要仅获取隐藏项,请使用 Hidden 参数、其“h”或“ah”别名或 Attributes 参数的 Hidden 值。若要排除隐藏项,请省略 Hidden 参数或使用 Attributes 参数。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-ReadOnly

仅获取只读文件和目录(文件夹)。

若要仅获取只读项,请使用 ReadOnly 参数、其“ar”别名或 Attributes 参数的 ReadOnly 值。若要排除只读项,请使用 Attributes 参数。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-System

仅获取系统文件和目录(文件夹)。

若要仅获取系统文件和文件夹,请使用 System 参数、其“as”别名或 Attributes 参数的 System 值。若要排除系统文件和文件夹,请使用 Attributes 参数。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-Force

获取隐藏的文件和文件夹。默认情况下,会排除隐藏的文件和文件夹。也可以通过使用 Hidden 参数或 Attributes 参数的 Hidden 值获取隐藏的文件和文件夹。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-UseTransaction

在活动事务中使用该命令。仅当正在执行事务时,此参数才有效。有关详细信息,请参阅 about_Transactions

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

<CommonParameters>

此 cmdlet 支持通用参数:-Debug、-ErrorAction、-ErrorVariable、-OutBuffer、-OutVariable、-Verbose、-WarningAction 和 -WarningVariable。有关详细信息,请参阅 about_CommonParameters

输入和输出

输入类型是指可通过管道传送给 cmdlet 的对象的类型。返回类型是指 cmdlet 所返回对象的类型。

输入

System.String[]

可以通过管道将文件系统路径(用引号引起来)传递给 Get-ChildItem。

输出

System.IO.DirectoryInfo, System.IO.FileInfo, System.String

注意

已在 Windows PowerShell 3.0 中引入了 Attributes、Directory、File、Hidden、ReadOnly 和 System 参数,这些参数

仅在文件系统驱动器中有效。

Get-ChildItem 别名参考:

---------------------------------

Get-ChildItem dir

Directory d, ad

File af

Hidden h, ah

ReadOnly ar

System as

示例 1

C:\PS>Get-ChildItem

Description
-----------
This command gets the files and subdirectories in the current directory. If the current directory does not have child items, the command does not return any results.





示例 2

C:\PS>Get-Childitem -System -File -Recurse

Description
-----------
This command gets system files in the current directory and its subdirectories.





示例 3

C:\PS>Get-ChildItem -Attributes !Directory,!Directory+Hidden

C:\PS> dir -att !d,!d+h

Description
-----------
These command get all files, including hidden files, in the current directory, but exclude subdirectories. The second command uses aliases and abbreviations, but has the same effect as the first.





示例 4

C:\PS>dir -ad

Description
-----------
This command gets the subdirectories in the current directory. It uses the "dir" alias of the Get-ChildItem cmdlet and the "ad" alias of the Directory parameter.





示例 5

C:\PS>Get-ChildItem -File -Attributes !ReadOnly -path C:\ps-test

Description
-----------
This command gets read-write files in the C:\ps-test directory.





示例 6

C:\PS>get-childitem . -include *.txt -recurse -force

Description
-----------
This command gets all of the .txt files in the current directory and its subdirectories. 

The dot (.) represents the current directory. The Include parameter specifies the file name extension. The Recurse parameter directs Windows PowerShell to search for objects recursively, and it indicates that the subject of the command is the specified directory and its contents. The Force parameter adds hidden files to the display.





示例 7

C:\PS>get-childitem c:\windows\logs\* -include *.txt -exclude A*

Description
-----------
This command gets the .txt files in the Logs subdirectory, except for those whose names start with the letter A. It uses the wildcard character (*) to indicate the contents of the Logs subdirectory, not the directory container. Because the command does not include the Recurse parameter, Get-ChildItem does not include the contents of the current directory automatically; you need to specify it.





示例 8

C:\PS>get-childitem -name

Description
-----------
This command retrieves only the names of items in the current directory.





See Also

Concepts

FileSystem 提供程序

Other Resources

Clear-Content
Get-Content
Get-ChildItem
Get-Content
Get-Item
Remove-Item
Set-Content
Test-Path