“%”(百分比)在PowerShell中做什么?
似乎%操作在pipe道之后启动脚本块,尽pipeabout_Script_Blocks指示%不是必需的。
这些都工作得很好。
get-childitem | %{ write-host $_.Name } { write-host 'hello' } %{ write-host 'hello' } 但是当我们在pipe道之后添加一个脚本块时,我们需要首先得到%。
 get-childitem | { write-host $_.Name } 
	
 当在cmdlet的上下文中使用(如您的示例)时,它是ForEach-Object的别名: 
 > Get-Alias -Definition ForEach-Object CommandType Name Definition ----------- ---- ---------- Alias % ForEach-Object Alias foreach ForEach-Object 
当在方程的上下文中使用时,它是模运算符 :
 > 11 % 5 1 
 和作为模数运算符, %也可以用在赋值运算符 ( %= )中: 
 > $this = 11 > $this %= 5 > $this 1 
 后PowerShell  – 特殊字符和令牌 
 提供多个符号的描述,包括% 
 % (percentage) 1. Shortcut to foreach. Task: Print all items in a collection. Solution. ... | % { Write-Host $_ } 2. Remainder of division, same as Mod in VB. Example: 5 % 2 
  %可以代替Get-ChildItem |  ForEach-Object { write-host $_.Name } ,不能没有%或ForEach-Object