powershell循环_带有示例的Powershell ForEach循环语句教程

powershell循环

powershell循环

ForEach is a PowerShell statement used to use iterate or loop over the given list, array or collection of the objects, strings, numbers, etc. ForEach is a very popular loop mechanism where we can use different cases like loop through files, Numbers, Strings, Processes, etc. Powershell version 4.0 brings some performance improvements to the ForEach loop.

ForEach是一个PowerShell语句,用于对指定列表,对象,字符串,数字等的列表,数组或集合进行迭代或循环。ForEach是一种非常流行的循环机制,在此机制中,我们可以使用不同的情况,例如遍历文件,数字,字符串,进程等。Powershell 4.0版为ForEach循环带来了一些性能改进。

PowerShell ForEach语法 (PowerShell ForEach Syntax)

Foreach statement has the following syntax where we use ITEM, COLLECTION and CODE-BLOCK.

在我们使用ITEMCOLLECTIONCODE-BLOCK的情况下,Foreach语句具有以下语法。

ForEach(ITEM in COLLECTION)
{
   CODE BLOCK
}
  • ITEM is a single value or object which changes over each iteration.

    ITEM是一个值或对象,它在每次迭代中都会变化。
  • COLLECTION is an array, collection, list of values, or objects like 1,2,3,4.

    COLLECTION是一个数组,集合,值列表或诸如1,2,3,4之类的对象。
  • CODE BLOCK is the block where the real operation runs which generally uses ITEM. This block can be single or multiple lines.

    代码块是实际操作运行的块,通常使用ITEM。 该块可以是单行或多行。

遍历字符串数组 (Foreach Through An Array Of Strings)

In a simple way, we can iterate over the given list of the string array. We iterate over the array named “cities” that holds multiple city names. We will print each city name with the command “echo” like below.

以一种简单的方式,我们可以遍历字符串数组的给定列表。 我们遍历包含多个城市名称的名为“ cities”的数组。 我们将使用命令“ echo”打印每个城市的名称,如下所示。

$cities = ("istanbul,ankara,canakkale")

foreach ($city in $cities){ echo $city}
ForeachThrough An Array Of Strings
ForeachThrough An Array Of Strings
ForeachThrough字符串数组

We can see that every city name is printed to the powershell terminal in a single line.

我们可以看到每个城市名称都在一行中打印到Powershell终端。

通过一组数字进行学习 (Foreach Through An Collection Of Numbers)

In the previous example, we have looped over a given list of strings. We can also iterate or loop through a list of numbers. We will create an array named numbers and iterate over each item in this number array.

在前面的示例中,我们遍历了给定的字符串列表。 我们还可以迭代或遍历数字列表。 我们将创建一个名为numbers的数组,并遍历此number数组中的每个项目。

$numbers=(1,2,3,4,5)
foreach ($number in $numbers){ echo $number}
Foreach Through An Collection Of Numbers
Foreach Through An Collection Of Numbers
通过一组数字进行学习

$ _用法($_ Usage)

When using some cmdlets to redirect the output of cmdlet to the Foreach we can use “$” for each item or object. For example, if we use “Get-Process” cmdlet each item or process will be assigned to the $ in each iteration. We can specify the attribute like Name $.Name  and print like below.

当使用某些cmdlet将cmdlet的输出重定向到Foreach时,我们可以对每个项目或对象使用“ $”。 例如,如果我们使用“ Get-Process” cmdlet,则在每次迭代中会将每个项目或进程分配给$ 。 我们可以指定诸如Name $.Name类的属性,并如下所示进行打印。

PS> Get-Process | ForEach-Object {$_.ProcessName}
$_ Usage
$_ Usage
$ _用法

We can also print multiple attributes in foreach by delimiting them with comma.

我们也可以通过用逗号分隔在foreach中打印多个属性。

PS> Get-Process | ForEach-Object {$_.ProcessName, $_.ID}
$_ Usage
$_ Usage
$ _用法

遍历文件(Foreach Through Files)

We can also loop through files by using Get-ChildItem cmdlet. Get-ChildItem will list files and folders like dir MSDOS command. This output will be written to console with the Echo command.

我们还可以使用Get-ChildItem cmdlet遍历文件。 Get-ChildItem将列出文件和文件夹,例如dir MSDOS命令。 该输出将通过Echo命令写入控制台。

foreach($file in Get-ChildItem) {Echo $file}
Foreach Through Files
Foreach Through Files
遍历文件

通过特定的文件扩展名(如Bat)进行Foreach(Foreach Through Specific File Extensions Like Bat)

We can specifically list the specified extension. The extension will generally provide the file type like text, avi, mkv, mp3, doc etc. In this example, we will list only *.bat or Bat files.

我们可以专门列出指定的扩展名。 该扩展名通常会提供文件类型,例如text,avi,mkv,mp3,doc等。在此示例中,我们将仅列出*.bat或Bat文件。

foreach($file in Get-ChildItem *.bat) {Echo $file}
Foreach Through Specific File Extensions Like Bat
Foreach Through Specific File Extensions Like Bat
通过特定的文件扩展名(如Bat)进行Foreach

We can see that only files like test.bat and autoexec.bat will be listed.

我们可以看到仅列出了诸如test.batautoexec.bat类的文件。

LEARN MORE  How To Create Nested For Loops In Python?
了解更多如何在Python中创建嵌套的for循环?

通过CSV文件进行Foreach (Foreach Over CSV File)

CSV files are used to store text data in a structured manner. We can easily foreach in each line of a CSV file like below. In this example, we will read an IP CSV file and provide to the foreach to print Powershell console.

CSV文件用于以结构化方式存储文本数据。 我们可以轻松地在CSV文件的每一行中进行foreach,如下所示。 在此示例中,我们将读取IP CSV文件并将其提供给foreach以打印Powershell控制台。

$IPs = (Get-Content IP.csv)[0].split(",")
foreach ( $IP in $IPs){
    echo $IP
}

翻译自: https://www.poftut.com/powershell-foreach-loop-statement-tutorial-with-examples/

powershell循环

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值