![get-childitem](https://i-blog.csdnimg.cn/blog_migrate/4667bf9a43e3f0548b74b33c3caaf8cc.png)
get-childitem
Get-ChildItem
is very useful cmdlet provided by Powershell. This cmdlet has two main usages. One is if the directory is provided as a parameter the given directory files and folders will be enumerated like a loop. Second is if a list or container is provided all the child will we enumerated. Let’s start action to learn details.
Get-ChildItem
是Powershell提供的非常有用的cmdlet。 此cmdlet有两个主要用法。 一种是如果将目录作为参数提供,则给定目录文件和文件夹将像循环一样被枚举。 第二个是如果提供了一个列表或容器,我们将枚举所有孩子。 让我们开始行动以了解细节。
句法 (Syntax)
Syntax is like below.
语法如下。
Get-ChildItem OPTIONS PATH
- OPTIONS used for different behaviors 用于不同行为的选项
- PATH is used to specify path without any option.PATH用于指定路径,不带任何选项。
帮帮我 (Help)
We can print help information about Get-ChildItem
cmdlet like below. We will use Get-Help
cmdlet to print help information about Get-ChildItem
.
我们可以像下面那样打印有关Get-ChildItem
cmdlet的帮助信息。 我们将使用Get-Help
cmdlet打印有关Get-ChildItem
帮助信息。
PS> Get-Help Get-ChildItem
![Help](https://i-blog.csdnimg.cn/blog_migrate/872b68d04bb1c2e64861b6a846ab78ad.png)
列出当前目录中的所有文件和文件夹(List All Files And Folders In Current Directory)
We will start with a simple example where we do not provide any option to the Get-ChildItem
cmdlet. This will print all files and folders in the current working directory.
我们将从一个简单的示例开始,在该示例中,我们不向Get-ChildItem
cmdlet提供任何选项。 这将打印当前工作目录中的所有文件和文件夹。
PS> Get-ChildItem
![List All Files And Folders In Current Directory](https://i-blog.csdnimg.cn/blog_migrate/21cd7ba7c520a69f27b74ae004cffe89.png)
We can see that the following information is provided.
我们可以看到提供了以下信息。
- Mode like read, write execute 读,写,执行等模式
- LastWriteTimeLastWriteTime
- Length which specifies the size 长度指定大小
- Name名称
仅显示给定扩展名(Shown Only Given Extension)
In this example, we will only show files with the specific extension. We can provide the extension after the cmdlet. In this case, we will list System file extension which can be specified as *.sys
like below.
在此示例中,我们将仅显示具有特定扩展名的文件。 我们可以在cmdlet之后提供扩展名。 在这种情况下,我们将列出系统文件扩展名,可以将其指定为*.sys
如下所示。
PS> Get-ChildItem *.sys
![Shown Only Given Extension](https://i-blog.csdnimg.cn/blog_migrate/354f831f3a2c995942b989ef3976140e.png)
We can also use -Path
option we can also provide the extension like below.
我们还可以使用-Path
选项,也可以提供如下扩展名。
PS> Get-ChildItem -Path *.sys
![Shown Only Given Extension](https://i-blog.csdnimg.cn/blog_migrate/dcc6acf0f4a21096afe8c3b63c4aadfa.png)
指定路径(Specify Path)
The default behavior of the Get-ChildItem
is running on the current working directory. If we want to run this cmdlet in the different path we can specify the path with the -Path
option. In this example, we will run on the Windows
directory in the C:
partition like below. Using double quotes will make the path specification more stable because spaces in the path may create problems.
Get-ChildItem
的默认行为是在当前工作目录上运行。 如果要在其他路径中运行此cmdlet,则可以使用-Path
选项指定路径。 在此示例中,我们将在C:
分区的Windows
目录下运行,如下所示。 使用双引号会使路径指定更稳定,因为路径中的空格可能会引起问题。
PS> Get-ChildItem -Path "c:\Windows"
![Specify Path](https://i-blog.csdnimg.cn/blog_migrate/256e585f3cfe9ff01bffbf7a036fb259.png)
递归列出(List Recursively)
The default behavior is listing current working directory or path to list. If we want to list the subfiles and subfolders we need to make the listing in a recursive manner. We can enable recursion with the -Recurse
option like below.
默认行为是列出当前工作目录或列表路径。 如果要列出子文件和子文件夹,则需要以递归方式列出。 我们可以使用-Recurse
选项启用递归,如下所示。
PS> Get-ChildItem -Recurse
![List Recursively](https://i-blog.csdnimg.cn/blog_migrate/2e6ea6e7eec0f97b4cbc301ccb810024.png)
We can see that there are some errors printed in red because we do not have access to this path.
我们可以看到以红色打印了一些错误,因为我们无法访问该路径。
指定递归深度 (Specify Recursion Depth)
By specifying recursion we will let the Get-ChildItem
command list all subfiles and subfolders. This means recursive list will go to the 100 level if it exists. This may create some performance or output problems. We can limit the depth of recursive listing with the -Depth
option. We will provide the level as a number like 5. In this example, we will only list depth with 1 like below.
通过指定递归,我们将让Get-ChildItem
命令列出所有子文件和子文件夹。 这意味着如果存在递归列表,它将达到100级。 这可能会产生一些性能或输出问题。 我们可以使用-Depth
选项来限制递归列表的深度。 我们将级别提供为数字,例如5。在本示例中,我们将仅列出深度为1,如下所示。
PS> Get-ChildItem -Recurse -Depth 2
排除一些文件 (Exclude Some Files)
While listing we may need to exclude some files according to their names or extensions. We will use -Exclude
option and provide the file or folder name. In this example, we will exclude according to their extension where will exclude files with *.sys
extension.
列出时,我们可能需要根据文件名或扩展名排除某些文件。 我们将使用-Exclude
选项并提供文件或文件夹的名称。 在此示例中,我们将根据扩展名排除在外,将排除扩展名为*.sys
文件。
PS> Get-ChildItem -Exclude "*.sys"
包含文件和扩展名 (Include Files and Extension)
While excluding some files according to their names and extensions we can also include some file names and extensions with the -Include
option. In this example we will exclude *.tmp
extension but include files with name check
even their extension is *.tmp
.
在根据文件名和扩展名排除某些文件时,我们也可以使用-Include
选项将某些文件名和扩展名包括在内。 在此示例中,我们将排除*.tmp
扩展名,但包括带有名称check
文件,即使其扩展名是*.tmp
。
PS> Get-ChildItem -Exclude "*.tmp" -Include "*check*"
列出所有注册表项 (List All Registry Key)
Get-ChildItem
cmdlet can work with the registry by using HKLM
to the -Path
option. We can list all registry keys like below. We will search in Software
hive in this example.
通过将HKLM
用于-Path
选项, Get-ChildItem
cmdlet可以与注册表一起使用。 我们可以像下面列出所有注册表项。 在此示例中,我们将搜索“ Software
配置单元。
PS> Get-ChildItem -Path HKLM:\Software
![List All Registry Key](https://i-blog.csdnimg.cn/blog_migrate/705d15db3b0cce2d18d606d9d0c2f93a.png)
列出所有证书(List All Certificates)
Another useful feature of the Get-ChildItem
cmdlet is listing currently installed and used certificates. We will use -Path
with the Cert
parameter like below.
Get-ChildItem
cmdlet的另一个有用功能是列出当前安装和使用的证书。 我们将使用-Path
和Cert
参数,如下所示。
PS> Get-ChildItem -Path Cert:\
![List All Certificates](https://i-blog.csdnimg.cn/blog_migrate/8c74d1e7db9567f7d9a2302ec88e3ddf.png)
仅列出文件和文件夹的名称(List Only Name of The Files and Folders)
The default behavior of the Get-ChildItem
cmdlet is listing files names with other attributes like size, mode etc. If we just want to list the names of the files we need to provide -Name
option like below.
Get-ChildItem
cmdlet的默认行为是列出具有其他属性(例如大小,模式等)的文件名。如果我们只想列出文件名,则需要提供-Name
选项,如下所示。
PS> Get-ChildItem -Name
![List Only Name of The Files and Folders](https://i-blog.csdnimg.cn/blog_migrate/2726cd1bb0156e19743b56fbbf665b6c.png)
翻译自: https://www.poftut.com/get-childitem-cmdlet-to-loop-files-and-results/
get-childitem