I'm working with a simple foreach loop in PowerShell where I have an array of server names. The issue I'm facing is that when I call a command for each server, the output for the first server isn't appearing right after the corresponding "Searching..." message. Instead, it seems like the output for the first server is getting combined with the output of the second server. Here's my code:
```powershell
$servers = 'ABC001','ABD001','ACD001'
foreach ( $item in $servers ) {
Write-Host "Searching $item..."
OpenSourceCmdLetToUseWithVendorSystem -search $item | Select-Object -Property userName,Address,platform
}
```
When I run this, the output looks like:
```
Searching ABC001...
Searching ABD001...
Searching ACD001...
```
I've tried limiting the array with index ranges like `[0]`, which works fine, but using ranges like `[0..1]` or `[1..2]` gives the same undesired output. Any thoughts on why this is happening?
3 Answers
It looks like the issue is connected to how the `Select-Object` output is being processed. When you use it, PowerShell is implicitly sending the results through `Format-Table`, and that can introduce delays in how items are displayed. Because of this, your `Write-Host` messages can get interleaved with the output, causing that first result to appear out of order. To fix this, you might want to pipe your output to `Out-Host` or `Format-Table` explicitly, which should make the output synchronous and fix the ordering issue. You can read more about it in this [related discussion](https://old.reddit.com/r/PowerShell/comments/17gdva6/powershell_noob_confused_about_script_lines/k6g22ik/).
On top of what SurfingGoldenElephant mentioned, you might consider this approach for your loop:
```powershell
$Results = foreach ( $item in $servers ) {
Write-Host "Searching $item..."
OpenSourceCmdLetToUseWithVendorSystem -search $item
}
$Results | Select-Object -Property userName,Address,platform
```
This way, you collect the results into a variable before processing them, which can sometimes help with output issues. Alternatively, if you want to manipulate each single result:
```powershell
$Results = foreach ( $item in $servers ) {
Write-Host "Searching $item..."
$SingleResult = OpenSourceCmdLetToUseWithVendorSystem -search $item
[PSCustomObject]@{
UserName = $SingleResult.userName
Address = $SingleResult.Address
Platform = $SingleResult.platform
}
}
$Results
```
This might not actually be your loop's fault. It's likely an issue with how the vendor cmdlet is managing output. Here are a couple of things to test:
- **Force enumeration**: Sometimes the pipeline doesn’t flush outputs until the next iteration, so make sure you’re forcing it.
- **Check Write-Host vs. Write-Output**: If the cmdlet is writing directly to the host rather than through the pipeline, that could create your issue.
- **Use Out-Host explicitly**: Piping your output to `Out-Host` can also help to keep everything synced up.
- **Vendor cmdlets can buffer outputs weirdly**: If this cmdlet is doing remote calls, consider wrapping it in `$()` or piping to `Out-Null` for better flushing.
Your loop looks fine; the vendor cmdlet might just be the root of the problem!

So, are you assuming the Vendor command is redirecting to Format-Table? Just checking!