如何使用PowerShell迭代文件?

时间:2022-09-23 07:38:17

I want to iterate over files in a folder using powershell script;

我想使用powershell脚本迭代文件夹中的文件;

1 个解决方案

#1


15  

Get-ChildItem | ForEach-Object { <# do what you need to do #> }

or shorter:

或更短:

gci | % { <# ... #> }

or if you want an explicit looping construct:

或者如果你想要一个显式的循环结构:

foreach ($file in Get-ChildItem) {
    # ...
}

Note however, that foreach will only run once all output from Get-ChildItem is collected. In my opinion most Powershell code should use the pipeline as much as possible.

但请注意,只有收集了Get-ChildItem的所有输出后,才会运行foreach。在我看来,大多数Powershell代码应尽可能使用管道。

#1


15  

Get-ChildItem | ForEach-Object { <# do what you need to do #> }

or shorter:

或更短:

gci | % { <# ... #> }

or if you want an explicit looping construct:

或者如果你想要一个显式的循环结构:

foreach ($file in Get-ChildItem) {
    # ...
}

Note however, that foreach will only run once all output from Get-ChildItem is collected. In my opinion most Powershell code should use the pipeline as much as possible.

但请注意,只有收集了Get-ChildItem的所有输出后,才会运行foreach。在我看来,大多数Powershell代码应尽可能使用管道。