ForEach-Object
Run a block on each object
Get-ChildItem -File | ForEach-Object { $_.Name }
- Works on
- WindowsLinuxmacOS
- PowerShell
- 5.1 and later
- Reference
- Microsoft Learn: ForEach-Object
Runs a script block once per object. $_ is the item. For a simple property, ForEach-Object Name (PS 3+) is enough.
If you are building a list in memory, a foreach loop can be clearer. The cmdlet shines in a pipeline.
Try this
Get-ChildItem -File | ForEach-Object { $_.Name.ToUpper() }
Coming from another shell
| Shell | What you might type |
|---|---|
| bash / zsh | for f in *; do echo "$f"; done |
| cmd.exe | for %f in (*) do @echo %f |
More in From bash and From cmd.