Powershell 中 ForEach 和 ForEach-Object 的区别 [英] Difference between ForEach and ForEach-Object in powershell

查看:100
本文介绍了Powershell 中 ForEach 和 ForEach-Object 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ForEachForEach-Object 之间有什么区别吗?

Is there any difference between ForEach and ForEach-Object ?

我有一个这样的小代码,工作正常

I have a small code like this, works fine

$txt = Get-Content 'C:\temp\000.txt'
$result = foreach ($line in $txt) {$line.replace(".ini","")}
$result | out-file 'c:\temp\001.txt'

但是,如果我使用ForEach-Object",则会出现错误....

But if i use 'ForEach-Object', I got errors....

$txt = Get-Content 'C:\temp\000.txt'
$result = foreach-object ($line in $txt) {$line.replace(".ini","")}
$result | out-file 'c:\temp\001.txt'

为什么?以及如何使用 ForEach-Object

Why ? and how to output the loop results by using ForEach-Object

推荐答案

foreachForEach-Object 的别名,但它似乎也是一个关键字(这是令人困惑).

foreach is an alias of ForEach-Object but it appears to also be a keyword (which is confusing).

您使用的foreach ($中的$){} 语法是help about_foreach.

foreach 作为 ForEach-Object 别名是 help ForEach-Object.

关键字 foreach$ 中的每个 $ 进行操作,如 () 位.

The keyword foreach operates over each $<item> in the $<collection> as given in the () bit.

别名 foreach/function ForEach-Object 对其作为输入接收的集合的每个项目进行操作.

The alias foreach/function ForEach-Object operates over each item of the collection it receives as input.

这篇关于Powershell 中 ForEach 和 ForEach-Object 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆