Powershell 中 ForEach 和 ForEach-Object 的区别 [英] Difference between ForEach and ForEach-Object in powershell
问题描述
ForEach
和 ForEach-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
推荐答案
foreach
是 ForEach-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屋!