- 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
廣告