# Powershell foreach

**URL:** <https://forums.powershell.org/t/powershell-foreach/9833>\
**Category:** PowerShell Help\
**Created:** [December 9, 2017, 8:09am UTC](https://forums.powershell.org/t/powershell-foreach/9833 "2017-12-09T08:09:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![matty21202](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@matty21202](https://forums.powershell.org/u/matty21202)\
**Post date:** [December 9, 2017, 8:09am UTC](https://forums.powershell.org/t/powershell-foreach/9833/1 "2017-12-09T08:09:38Z")

</div>

Hi,

I am doing a simple foreach loop, although how do I add some text after the write host? after ($user.Surname) I would like to put some text such as $user.surname is currently enabled.

Get-ADUser -Filter \* | where {$\_.enabled -eq $true} | Select-Object `  
Name, Surname, Userprincipalname | Export-Csv “C:\enabledusersv2.csv”

$users = Import-Csv “c:\enabledusersv2.csv”

Foreach ($user in $users)  
{  
write-host ($user.Surname) -ForegroundColor Cyan  
}

Apologies for the simple question, I am starting from the beginning!

thank you

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 9, 2017, 8:44am UTC](https://forums.powershell.org/t/powershell-foreach/9833/2 "2017-12-09T08:44:34Z")

</div>

OK, first: please format your code as code. It makes yours and our lifes easier. Second: we try to avoid Write-Host. [Everytime you use Write-Host a puppy dies](http://www.youtube.com/watch?v=SSJot1ycM70&t=21m30s). 😉 If you don’t care about all these cute puppies you can do it like this:

```
Get-ADUser -Filter * |
Where-Object {$_.enabled} |
Select-Object -Property Name, Surname, Userprincipalname |
Export-Csv “C:\enabledusersv2.csv”

$users = Import-Csv “c:\enabledusersv2.csv”  
Foreach ($user in $users){  
Write-Host “‘$($user.Surname)’ is currently enabled” -ForegroundColor Cyan  
}
```
  
Some more tips: Try to [avoid back ticks, indent your code](http://www.youtube.com/watch?v=SSJot1ycM70&t=15m10s) and try to avoid aliasses and abbreviations. This will make your code easier to read and to understand for yourself and for others.

---

<div class="post-metadata">

**Author:** ![matty21202](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@matty21202](https://forums.powershell.org/u/matty21202)\
**Post date:** [December 9, 2017, 9:14am UTC](https://forums.powershell.org/t/powershell-foreach/9833/3 "2017-12-09T09:14:50Z")

</div>

thank you! I am hopeless at powershell and I can’t even get a foreach correct so I have no hope!! 🙂

even if I try to do

```
Get-ADUser -Filter * | Select-Object -Property Name, PasswordNeverExpires
```

it gives me the correct name of the users… but then the passwordneverexpires column doesn’t show anything…  
I confirmed it is $true by just doing get-aduser -filter \* -properties \* and it shows as they have information next to the property passwordneverexpires…

thanks again.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 9, 2017, 9:22am UTC](https://forums.powershell.org/t/powershell-foreach/9833/4 "2017-12-09T09:22:48Z")

</div>

Get-ADUser only outputs a subset of properties by default. If you want to have more than that you have to specify it.

```
Get-ADUser -Filter * -Properties PasswordNeverExpires | Select-Object -Property Name, PasswordNeverExpires
```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 9, 2017, 9:25am UTC](https://forums.powershell.org/t/powershell-foreach/9833/5 "2017-12-09T09:25:29Z")

</div>

You might start with learning the basics of Powershell first …  
[Microsoft Virtual Academy - Getting Started with Microsoft PowerShell](https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276)  
There you even learn how to use the help and how to help yourself. 😉

---

<div class="post-metadata">

**Author:** ![rob-jaudon](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@rob-jaudon](https://forums.powershell.org/u/rob-jaudon)\
**Post date:** [December 11, 2017, 10:10am UTC](https://forums.powershell.org/t/powershell-foreach/9833/6 "2017-12-11T10:10:33Z")

</div>

Matt,

I understand your pain. Believe me. I am also a n00b to PS and learning something new everyday. Take it in steps and just try something simple to complete each day. I can tell you the book series Learn PS in a month of lunches will help you. It helped me tremendously. Investing in that book will clear things up and get you going.

Also these forums are a huge help.

Rob

---

<div class="post-metadata">

**Author:** ![matty21202](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@matty21202](https://forums.powershell.org/u/matty21202)\
**Post date:** [December 11, 2017, 4:02pm UTC](https://forums.powershell.org/t/powershell-foreach/9833/7 "2017-12-11T16:02:52Z")

</div>

Thanks rob!! Is that book better than the YouTube series? If it’s exactly the same, is it handy having the book rather than keep using YouTube for the content?

Good luck with the powershell learning :).

---

<div class="post-metadata">

**Author:** ![shihanpietersz](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@shihanpietersz](https://forums.powershell.org/u/shihanpietersz)\
**Post date:** [December 11, 2017, 4:18pm UTC](https://forums.powershell.org/t/powershell-foreach/9833/8 "2017-12-11T16:18:56Z")

</div>

Hi Matt,  
We all are learning and all had to start some ware, don’t give up. Like Rob Had mentioned The books on Learn PS in a month of lunches are extremely valuable. Also Windows PowerShell in Action, Third Edition is a good book which goes in depth. for video training Microsoft virtual academy is a good start as its free. But if you or your company has the funds a subscription to Plural sight would do you real good. They have videos from Beginner to expert level and the trainers are the best in the business.

Good luck !

---

<div class="post-metadata">

**Author:** ![jarkko](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@jarkko](https://forums.powershell.org/u/jarkko)\
**Post date:** [December 12, 2017, 4:17am UTC](https://forums.powershell.org/t/powershell-foreach/9833/9 "2017-12-12T04:17:17Z")

</div>

Hi

Sorry Olaf to say this (I must admit that I adore your scripting) but for Get-AdUser and -Filter \* is not best way to show as an example. This is taking a lot of time to run and if there is multiple users the script is taking forever versus typing what you want like in this case passwordNeverExpires.

Regards

Jake

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 12, 2017, 6:44am UTC](https://forums.powershell.org/t/powershell-foreach/9833/10 "2017-12-12T06:44:04Z")

</div>

Jarkko,  
I just took the example Matt already used and tried to improve it. As you can see I specified the additional property and the filter criteria was taken from Matt. I would even recommend to limit the search with a SearchBase but I didn’t want to confuse Matt more than nessecary. 😉

---

<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/powershell-foreach/9833/11 "2024-05-16T20:37:02Z")

</div>

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


