Get-ChildItem cmdlet gets all of the files in the current folder and then
uses the pipeline operator (
|) to send those files to Rename-Item.
It sends the files one-by-one, each as a separate item,
Rename-Item also receives them one-by-one and acts upon each one
In the script block, the
$_ automatic variable represents each file object as it comes to the command through the pipeline, so it can act upon it.
This behavior is very different from that of the operating system,
where the first command in the pipeline produces all the output as one list,
which is then passed as one block via the pipeline.
The PowerShell philosophy of pipelines has these objectives :
- Make each program do one thing well
- Expect the output of every program to become the input to another
This way, you may string programs, where each one does one thing well,
without requiring the script to use artificial objects such as lists.
Such complex objects or lists would require parsing,
which PowerShell avoids by passing items instead of lists.
ForEach-Object is not required because the concept of the
PowerShell pipeline is designed specifically to avoid loops that would
There is no implied loop here, there are only two programs, one generating items
that are handled by the next one in the pipeline.