get-childitem_Get-ChildItem Cmdlet循环文件和结果

get-childitem

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
Help
帮帮我

列出当前目录中的所有文件和文件夹(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
List All Files And Folders In Current Directory
列出当前目录中的所有文件和文件夹

We can see that the following information is provided.

我们可以看到提供了以下信息。

  • Mode like read, write execute

    读,写,执行等模式
  • LastWriteTime

    LastWriteTime
  • 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
Shown Only Given Extension
仅显示给定扩展名

We can also use -Path option we can also provide the extension like below.

我们还可以使用-Path选项,也可以提供如下扩展名。

PS> Get-ChildItem -Path *.sys
Shown Only Given Extension
Shown Only Given Extension
仅显示给定扩展名

指定路径(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
Specify Path
指定路径

递归列出(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
List Recursively
递归列出

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
List All Registry Key
列出所有注册表项

列出所有证书(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的另一个有用功能是列出当前安装和使用的证书。 我们将使用-PathCert参数,如下所示。

PS> Get-ChildItem -Path Cert:\
List All Certificates
List All Certificates
列出所有证书

仅列出文件和文件夹的名称(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
List Only Name of The Files and Folders
仅列出文件和文件夹的名称

翻译自: https://www.poftut.com/get-childitem-cmdlet-to-loop-files-and-results/

get-childitem

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值