Commands / Text and objects

ForEach-Object

Run a block on each object

beginner Text and objects Aliases foreach%

ForEach-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

ShellWhat you might type
bash / zshfor f in *; do echo "$f"; done
cmd.exefor %f in (*) do @echo %f

More in From bash and From cmd.

Related commands