- PowerShell 教程
- PowerShell - 主页
- PowerShell - 概述
- PowerShell - 环境设置
- PowerShell - Cmdlet
- PowerShell - 文件和文件夹
- PowerShell - 日期和计时器
- PowerShell - 文件 I/O
- PowerShell - 高级 Cmdlet
- PowerShell - 脚本编写
- PowerShell - 特殊变量
- PowerShell - 运算符
- PowerShell - 循环
- PowerShell - 条件
- PowerShell - 数组
- PowerShell - 哈希表
- PowerShell - Regex
- PowerShell - 反引号
- PowerShell - 括号
- PowerShell - 别名
- PowerShell 有用资源
- PowerShell - 快速指南
- PowerShell - 有用资源
- PowerShell - 讨论
PowerShell - ForEach-Object Cmdlet
Cmdlet
可以使用ForEach-Object cmdlet 对对象集合的每个对象执行操作。
在这些示例中,我们将看到 ForEach-Object cmdlet 的操作。
示例 1
在这个示例中,我们将除以数组中的整型。我们将使用 $_ 引用每个对象。
1000,2000,3000 | ForEach-Object -Process {$_/1000}
输出
你可以在 PowerShell 控制台中看到以下输出。
1 2 3
示例 2
获取当前目录中项目的名称。
在这个示例中,我们将拆分 PowerShell 模块名称。
"Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" | ForEach-Object {$_.Split(".")}
输出
你可以在 PowerShell 控制台中看到以下输出。
Microsoft PowerShell Core Microsoft PowerShell Host
powershell_advanced_cmdlets.htm
广告