Still learning how to use PowerShell

That is not helpful at all. Both commands must have outputted error messages.

When you get error messages you should share them completely along with the code you used.

And as always: When you post code, sample data, console output or error messages, please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance

The user principal name usually is built automatically by concatenating the sAMAccountName and the domain of the AD domain.

If you want to use the cmdlet Set-ADUser you have to provide an identity to specify the account you want to change. You can do this either by piping an object towards Set-ADUser or you have use the parameter -Identity. In both of your cases you did not provide an identity.

You should always read the help for the cmdlets you’re about to use completely including the examples to learn how to use them.

Additionally your syntax to provide the Name property of the pipeline variable is wrong. Instead of $_Name it should be $_.Name. And you have to provide the domain part in quotes.

So in the simplest case you could run something like this:

Get-ADUser -Filter * | 
    Foreach-Object { Set-ADUser -Identity $_.sAMAccountName -UserPrincipalName ($_.Name + '@MyDomain.com')}

To reduce the stress you put on your AD you should consider using the parameter -SearchBase to specify a search base. :wink:

1 Like