This one has me stumped. I’ve written some complicated scripts in my time using Powershell, some of which I am very proud of.
I thought this one would be simple. Alas not, or I am missing something obvious!
I need to modify the UPN on a bunch of people’s AD user accounts. Unfortunately the user accounts have been provided to me via a list of email addresses. No biggie I thought, I can just write a simple script to get user accounts based on the email address attribute (which is present on all our users) and export that to a new CSV file, and go from there.
I used the following command to obtain the AD account info for a single user:
Get-ADUser -Filter {mail -eq 'email@domain.com'}
This works fine; returns a user account purely on the email address using the filter. I’m now applying this command to an imported CSV thus:
Ok, did some testing on my end here. Be sure that you are importing your csv correctly first, by doing this command alone:
Import-CSV csvfile.csv
When I tested, it didn’t work because the delimiter was wrong, so I had to use the -Delimiter parameter. When you are certain that you are reading the CSV right, try this:
So just a case of expanding the variable correctly so that Powershell recognises it… the only real change there was to build out the $userMail variable first.
Noted for next time - this will come in handy as I’ve just had a load more stuff given to me, this time on firstname, surname. Fun!!