Add a UPN

by NeatAccurateTypist at 2013-03-11 14:54:08

I used Import-CSV to create a load of AD accounts today but forgot to add the UserPrincipalName property.

I realise I should have added something like: -UserPrincipalName ($.samAccountName + ‘’) in my original command, but now I’m trying to add the UPN to the newly-created accounts.

I’m trying something along the lines of:

Get-ADUser -filter * -SearchBase ‘OU=OrganizationlUnit,DC=domain,DC=com’ | Set-ADUser -UserPrincipalName …

What’s the equivalent of "($
.samAccountName + ‘’)" for accounts that already exist, but have no UPN?

by DonJ at 2013-03-13 08:00:25
You need to chuck in a ForEach.

Get-ADUser -filter * | ForEach { Set-ADUser }

Within the {} you’ll have $_ again.
by NeatAccurateTypist at 2013-03-13 08:36:58
Thanks I’ve tried this but I’m getting this reply:

cmdlet Set-ADUser at command pipeline position 1
by DonJ at 2013-03-13 08:40:27
Ok, so show me the exact command you tried to run that gave you that error.
by NeatAccurateTypist at 2013-03-13 09:00:31
I’ve been trying a few things, with and without parentheses within the curly brackets, along the lines of:

Get-ADUser -filter * | ForEach { Set-ADUser -UserPrincipalName ($.samAccountName + ‘’) }
by MasterOfTheHat at 2013-03-13 09:36:08
I don’t have a test domain to verify it on, but it should be as simple as:
Get-ADUser -filter * | ForEach { Set-ADUser -UserPrincipalName "$($
.samAccountName + '')" }

Note the quotes and extra $ in the -UserPrincipleName parameter…
by NeatAccurateTypist at 2013-03-13 09:47:57
Thanks MasterOfTheHat,

I’ve tried your suggestion, but I’m still getting the following message:

cmdlet Set-ADUser at command pipeline position 1
Supply values for the following parameters:
by MasterOfTheHat at 2013-03-13 10:45:18
AH! Makes sense… The Set-ADUser command doesn’t know what user object to apply the UPN to! Sorry I forgot that. Try:
Get-ADUser -filter * | ForEach { Set-ADUser -Identity "$.samAccountName" -UserPrincipalName "$($.samAccountName + '')" }
by NeatAccurateTypist at 2013-03-14 05:57:20
Thanks MasterofTheHat!

I tried that and it needed a slight adjustment (I had to remove the quotes from around the first "$_.samAccountName" and then it ran perfectly).

Did exactly what I needed! Thanks again!