# foreach confusion

**URL:** <https://forums.powershell.org/t/foreach-confusion/1914>\
**Category:** PowerShell Help\
**Created:** [September 25, 2013, 6:03am UTC](https://forums.powershell.org/t/foreach-confusion/1914 "2013-09-25T06:03:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![barry-thomson](https://avatars.discourse-cdn.com/v4/letter/b/ecb155/32.png) [@barry-thomson](https://forums.powershell.org/u/barry-thomson)\
**Post date:** [September 25, 2013, 6:03am UTC](https://forums.powershell.org/t/foreach-confusion/1914/1 "2013-09-25T06:03:11Z")

</div>

Hi,

Anyone able to help a nooby? I thought I had a handle on the foreach loop, but when I came to put it into practice, it proved I didn’t…

This is what I’ve got so far…any idea what I’m missing? Do I need to specify a comuter paramater after Get-WmiObject win32\_networkadapter?

Thank again

$machines = Read-Host “Location and file name of your input”  
$filename = Read-Host “Enter a file name for the output”

foreach ($machine in $machines) {

```
Get-WmiObject win32_networkadapter -ErrorAction Ignore | 

where {$_.physicaladapter -eq $true} | 

Select-Object macaddress, netconnectionid, PSComputerName |

Export-Csv -Path $env:USERPROFILE\$filename -NoTypeInformation

                      }

```

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [September 25, 2013, 6:10am UTC](https://forums.powershell.org/t/foreach-confusion/1914/2 "2013-09-25T06:10:49Z")

</div>

Well…

$machines is getting a single string. You’re asking for a “location and file name of your input.” The foreach loop will only have that one string to enumerate through, and it won’t be a computer name. Was your intent to read computer names from a file?

```
$inputfile = Read-Host "Enter input filename, must be one computer name per line"
$computers = Get-Content $inputfile
foreach ($computer in $computers) {
}
```

Second, yes, you do actually have to use the computer name with Get-WmiObject. The command defaults to querying the local computer.

```
foreach ($computer in $computers) {
  Get-WmiObject -Class Win32_Whatever -ComputerName $computer
}
```

That’s how you’d USE the $computer variable from the foreach loop.

Additionally, you’re not going to like the output when you send each computer through Export-CSV like that. You’re going to end up overwriting the file, so it’ll only contain the last computer processed. A typical approach would be to handle the processing and the output as separate steps.

```
function Get-Stuff {
  $inputfile = Read-Host "Enter input filename, must be one computer name per line"
  $computers = Get-Content $inputfile
  foreach ($computer in $computers) {
    Get-WmiObject -Class Win32_Whatever -ComputerName $computer
  }
}
Get-Stuff | Export-CSV outputfile.csv
```

Or even better, make the input a mandatory parameter so the shell will prompt for it:

```
function Get-Stuff {
  Param([Parameter(Mandatory=$True)][string]$inputfile)
  $computers = Get-Content $inputfile
  foreach ($computer in $computers) {
    Get-WmiObject -Class Win32_Whatever -ComputerName $computer
  }
}
Get-Stuff | Export-CSV outputfile.csv
```

Also, “Ignore” is not a valid ErrorAction. You probably mean “SilentlyContinue.” Run “help about\_common\_param\*” in the shell for more details. [EDIT: Was reminded Ignore is valid in v3; I’ve still got my head in v2 this morning]

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [September 25, 2013, 6:17am UTC](https://forums.powershell.org/t/foreach-confusion/1914/3 "2013-09-25T06:17:39Z")

</div>

Ignore is valid in PowerShell v3, actually. It behaves like SilentlyContinue, except it also doesn’t add anything to the $error collection.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [September 25, 2013, 6:21am UTC](https://forums.powershell.org/t/foreach-confusion/1914/4 "2013-09-25T06:21:26Z")

</div>

And to perhaps offer a more academic look at the ForEach loop itself: You need to start with a variable that contains one or more objects, such as strings.

```
$machines = @('SERVER1','SERVER2','SERVER3')
```

For example. Read-Host can actually only prompt for a single string, so it can’t generate a collection of objects like that. Get-Content produces a collection, with each line in the file being a single object.

```
ForEach ($machine in $machines) {
}
```

Takes everything in $machines (remember, it contains 1+ objects) and executes the loop one time “for each” of those objects. Each time through the loop, one object gets plucked out of $machines and stuffed into $machine. That way, inside the loop, you USE $machines to refer to just one thing at a time.

```
ForEach ($machine in $machines) {
  Get-WmiObject Win32_BIOS
}
```

That’d query your local computer one time “for each” object in $machines. Why your local computer? Because that’s what you told it to do. Being inside a ForEach loop doesn’t magically make commands act differently. THEY don’t know that $machines contains computer names. And not all commands can talk to remote computers.

```
ForEach ($machine in $machines) {
  Get-WmiObject Win32_BIOS -Comp $machine
}
```

Assuming $machines contains computer names and not, say, AD users, that example would query WMI on each of the machines.

---

<div class="post-metadata">

**Author:** ![barry-thomson](https://avatars.discourse-cdn.com/v4/letter/b/ecb155/32.png) [@barry-thomson](https://forums.powershell.org/u/barry-thomson)\
**Post date:** [September 25, 2013, 6:32am UTC](https://forums.powershell.org/t/foreach-confusion/1914/5 "2013-09-25T06:32:57Z")

</div>

Don, thanks for very much. Enjoying your book too! 🙂

Thank you

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:57pm UTC](https://forums.powershell.org/t/foreach-confusion/1914/6 "2024-05-16T20:57:02Z")

</div>

Closed on May 16, 2024, 8:57pm UTC


