Author Posts

December 9, 2017 at 1:39 pm

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

December 9, 2017 at 2:14 pm

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. 😉 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.

December 9, 2017 at 2:44 pm

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.

December 9, 2017 at 2:52 pm

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

December 9, 2017 at 2:55 pm

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. 😉

December 11, 2017 at 3:40 pm

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

December 11, 2017 at 9:32 pm

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

December 11, 2017 at 9:48 pm

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 !

December 12, 2017 at 9:47 am

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

December 12, 2017 at 12:14 pm

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. 😉