# using var and out-file in for each loop

**URL:** <https://forums.powershell.org/t/using-var-and-out-file-in-for-each-loop/9158>\
**Category:** PowerShell Help\
**Created:** [August 9, 2017, 2:34am UTC](https://forums.powershell.org/t/using-var-and-out-file-in-for-each-loop/9158 "2017-08-09T02:34:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![baschi](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@baschi](https://forums.powershell.org/u/baschi)\
**Post date:** [August 9, 2017, 2:34am UTC](https://forums.powershell.org/t/using-var-and-out-file-in-for-each-loop/9158/1 "2017-08-09T02:34:17Z")

</div>

Hey guys, yesterday I scripted some ps code and got some issues with it, I were able to solve it but I still have some questions in my head which I would like to get answered. I hope somebody can help 🙂

$csv=import-csv -path DEHEREMEX48\_gotactivated\_listpart.txt -Delimiter “;”

foreach ($line in $csv){  
get-aduser -filter {name -like $line.Name} -Properties (‘Name’, ‘co’)| Select Name, co  
}  
didn’t work, I got an system.Object error why do I have to transform the var using $changeme=line.Name before I’m able to use it in the loop:

foreach ($line in $csv){  
$changeme=$line.Name  
get-aduser -filter {name -like $changeme} -Properties (‘Name’, ‘co’)| Select Name, co  
}

1. The output of above delivers a nice table like  
name co  
xyz DE  
yyx DE  
Do I use a \>\> to write the output to a file like:  
foreach ($line in $csv){  
$changeme=$line.Name  
get-aduser -filter {name -like $changeme} -Properties (‘Name’, ‘co’)| Select Name, co \>\>resul.txt  
}

I’m getting a table like this one  
name co  
xyz DE  
name co  
yyx DE  
I know the workaround is to load the foreachloop into a var. That’s working for me, but I’m still asking myself why I get with the first command a nice table and with the “\>\>restult.txt” added a really crappy one.

Best Regards,  
baschi

---

<div class="post-metadata">

**Author:** ![simbrook](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@simbrook](https://forums.powershell.org/u/simbrook)\
**Post date:** [August 9, 2017, 2:52am UTC](https://forums.powershell.org/t/using-var-and-out-file-in-for-each-loop/9158/2 "2017-08-09T02:52:45Z")

</div>

I would use export-csv to output the data then you can import the results into a database or excel and do further manipulation

---

<div class="post-metadata">

**Author:** ![baschi](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@baschi](https://forums.powershell.org/u/baschi)\
**Post date:** [August 9, 2017, 5:37am UTC](https://forums.powershell.org/t/using-var-and-out-file-in-for-each-loop/9158/3 "2017-08-09T05:37:45Z")

</div>

thanks

```
foreach ($line in $csv){
$changeme=$line.Name
get-aduser -filter {name -like $changeme} -Properties ('Name', 'co')| Select Name, co | export-csv -path testdel.csv
}
```

doesn’t give a output in the csv at all.

---

<div class="post-metadata">

**Author:** ![simbrook](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@simbrook](https://forums.powershell.org/u/simbrook)\
**Post date:** [August 9, 2017, 5:45am UTC](https://forums.powershell.org/t/using-var-and-out-file-in-for-each-loop/9158/4 "2017-08-09T05:45:11Z")

</div>

try giving the csv a full path i.e. Export-csv c:\temp\testdel.csv

---

<div class="post-metadata">

**Author:** ![baschi](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@baschi](https://forums.powershell.org/u/baschi)\
**Post date:** [August 9, 2017, 7:10am UTC](https://forums.powershell.org/t/using-var-and-out-file-in-for-each-loop/9158/5 "2017-08-09T07:10:42Z")

</div>

still not. But thanks for your help. To be honest this is not what I’m searching for I already have an workaround with the variable I want to get an explanation as written in the startpost

---

<div class="post-metadata">

**Author:** ![david-schmidtberger](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@david-schmidtberger](https://forums.powershell.org/u/david-schmidtberger)\
**Post date:** [August 9, 2017, 7:55am UTC](https://forums.powershell.org/t/using-var-and-out-file-in-for-each-loop/9158/6 "2017-08-09T07:55:40Z")

</div>

while i don’t have a good explanation, i’ll wait for Don or one of the real experts.

my process for this would be to do $($line.Name) instead of just $line.Name

this always works for me

---

<div class="post-metadata">

**Author:** ![simbrook](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@simbrook](https://forums.powershell.org/u/simbrook)\
**Post date:** [August 9, 2017, 9:22am UTC](https://forums.powershell.org/t/using-var-and-out-file-in-for-each-loop/9158/7 "2017-08-09T09:22:46Z")

</div>

" i’ll wait for Don or one of the real experts"

Does this mean that Don is not one of the experts 🙂 lol

---

<div class="post-metadata">

**Author:** ![franktuckerca](https://avatars.discourse-cdn.com/v4/letter/f/5e9695/32.png) [@franktuckerca](https://forums.powershell.org/u/franktuckerca)\
**Post date:** [August 9, 2017, 2:12pm UTC](https://forums.powershell.org/t/using-var-and-out-file-in-for-each-loop/9158/8 "2017-08-09T14:12:06Z")

</div>

I THINK it just a limitation with -Filter not the looping.  
Believe it will work with -Identity

---

<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:37pm UTC](https://forums.powershell.org/t/using-var-and-out-file-in-for-each-loop/9158/9 "2024-05-16T20:37:44Z")

</div>

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


