How to Loop Through Files in a PowerShell Script

How to Loop Through Files in a PowerShell Script

Looping through files in a PowerShell script lets you perform the same action on each file in a folder, automating repetitive per-file tasks. Windows 11’s PowerShell makes iterating over files clean and readable, a common pattern in useful scripts.

The Command

foreach ($file in Get-ChildItem "C:\Docs\*.txt") { Write-Output $file.Name }

What It Does

This loops over each text file in the Docs folder. `Get-ChildItem` gathers the files, and `foreach` runs the code in braces once per file, with `$file` holding the current one each time. Here it simply outputs each file’s name, YYGACOR Login but you could perform any action, applying the same operation to every file in turn.

When You’d Use This

This is a common and powerful pattern for automating per-file tasks, such as processing, renaming, copying, or reading every file in a folder. When you need to apply the same operation to many files, looping handles them all in one script rather than running a command per file, which is exactly the kind of repetitive work automation is meant to eliminate.

Useful Variations

Inside the loop, you can copy, rename, read, or process each file using `$file`, which represents the current file object with properties like Name, FullName, and Length. To loop with the pipeline instead, `Get-ChildItem *.txt | ForEach-Object { … }` achieves similar results using `$_` for the current item within the block.

If It Doesn’t Work

If the loop does not act on the files you expect, test it first with a harmless action like outputting names to confirm it targets the right files before making changes. Access each file’s properties through the loop variable, such as `$file.FullName` for the full path when passing files to other commands. If nothing happens, the `Get-ChildItem` pattern may match no files, so check it separately.

Good to Know

The loop variable, here `$file`, gives access to each file’s properties, so `$file.FullName` provides the complete path and `$file.Name` just the filename, which matters when passing files to other commands. Testing the loop with a harmless action like outputting names first confirms it targets the right files before you have it make changes.

Putting It Together

The command shown may look dense at first, but it breaks down into clear parts once you have used it a few times. As part of working with output and building simple automation, this command is a building block you will reuse constantly. As you combine it with the other scripting basics here, small one-off commands grow into reusable scripts that save real time on repetitive work. Like anything in the terminal, the real value comes from trying it on your own system and adapting the variations above to what you actually need, so it is worth experimenting with in a safe, low-stakes situation before relying on it in a script or during troubleshooting. Keeping a note of the commands you find most useful, along with the variations that fit your workflow, turns scattered one-off tricks into a personal reference you can draw on whenever a similar task comes up again.

Leave a Reply

Your email address will not be published. Required fields are marked *