Powershell foreach


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

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. :wink: 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 and try to avoid aliasses and abbreviations. This will make your code easier to read and to understand for yourself and for others.

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

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.

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

You might start with learning the basics of Powershell first …
Microsoft Virtual Academy - Getting Started with Microsoft PowerShell
There you even learn how to use the help and how to help yourself. :wink:


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.


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 :).

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 !


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.



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. :wink: