PowerShell Foreach Loop: Efficient Batch Processing for Admins

Managing dozens of computers and tasks can feel like juggling flaming keyboards. Luckily, PowerShell gives IT admins superpowers. One of the best tools in the PowerShell toolkit? The Foreach loop. It’s simple, powerful, and lets you repeat actions without breaking a sweat.

Let’s break it down like a good playlist — fun and repeatable.

What is a Foreach Loop?

Think of it like this: You have a list of things — files, computers, usernames. You want to do something to each of those. A Foreach loop steps through each item one by one, letting you run the same command on all of them.

$Servers = "PC1", "PC2", "PC3"

foreach ($Server in $Servers) {
    Restart-Computer -ComputerName $Server -Force
}

That little script restarts three computers. Magic. Well, PowerShell magic.

Why is it So Useful for Admins?

Admins love the Foreach loop because:

  • It saves time
  • Reduces errors
  • Keeps scripts clean
  • Keeps you from losing your mind doing the same thing 100 times

Need to remove a stale user from 50 folders? Foreach. Need to copy a config file to 30 servers? Foreach. It’s the Swiss Army knife of automation.

Two Flavors: Foreach vs Foreach-Object

There’s actually two ways to write Foreach loops in PowerShell:

1. Foreach Statement (Loop)

This is the traditional one. Works great when you already have a list of items, like folders or computers.

$Users = Get-Content "C:\userlist.txt"

foreach ($User in $Users) {
    Write-Host "Processing $User"
}

2. Foreach-Object Cmdlet

This version pipes the data straight into the loop. Super efficient for real-time processing.

Get-ChildItem "C:\Logs" | ForEach-Object {
    $_.Name
}

See that $_? It’s a placeholder for the current item.

Common Tasks Made Easy

Here are some everyday things you can automate with Foreach:

  • Restart multiple computers
  • Delete temporary files across user profiles
  • Set permissions on many folders
  • Check service statuses
  • Create user accounts from a CSV

If you do it more than twice a week, Foreach it!

Tips for Power Users

Want to go from good to great? Try these tips:

  • Add error handling with Try / Catch
  • Use -Parallel with ForEach-Object in PowerShell 7+
  • Combine with Where-Object for filtering
  • Write results to a log file using Out-File

Here’s a quick ninja script:

$computers = Get-Content "C:\PCs.txt"

foreach ($pc in $computers) {
    Try {
        Get-Service -ComputerName $pc -Name "wuauserv"
    }
    Catch {
        Write-Host "Could not reach $pc"
    }
}

Short, sweet, solid.

Gotchas to Watch For

Foreach is amazing, but keep these in mind:

  • Large loops can be slow — use parallel processing when possible
  • Spelling mistakes in list data = script breaks
  • Always test with a small group first!

When used wisely, Foreach won’t let you down.

Wrap-Up: Loop It Like a Pro

The PowerShell Foreach loop is more than just a way to repeat actions. It’s a way to work smarter. It takes repetitive tasks and turns them into automatic routines.

So next time you stare at a list of servers, folders, or files, just remember: “If it’s repeatable, Foreach it.”

Happy scripting, captain!