Still learning how to use PowerShell

Hello everyone!
I am learning to use PowerShell little by little. I recently install AD in my lab environment to practice. I created many users from imported .csv file. I left out the UserPrincipalName so I am trying to add it via PowerShell.
I tried 2 ways

Get-ADUser -Filter * | Foreach {Set-aduser -UserPrincipalName ($}`*

Second try

$domain = “
$UPNs = get-aduser -Filter * | Select -Property *
Foreach ($UPN in $UPNs) { set-aduser -UserPrincipalName “Name + $domain”}``

None of them work.
I am more interested in knowing why these 2 won’t work? Thanks for helping me understand this.

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 + '')}

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

1 Like

I have some questions.

What are the differences between Objects and properties in PowerShell.? Can we just take a property and put a $_. in front of it and make it an object? Also where can I get a good explanation about these @{ hash tables that have some label and expressions?

Thanks for your time.

I asked you to format your code!! Because you did not there are some characters missing. And I cannot be sure about if it was the forum software or you.

I asked you to post errors along with the code you used. Errors are an important feature of a scripting or programming language. They tell you what’s wrong. You should carefully read trhem and try to understand.

And I asked you as well to read the help completely for the cmdlets you’re about to use.
The help for the parameter -Identity of the cmdlet Set-ADUser tells you this:


Specifies an Active Directory user object by providing one of the following property values. The identifier in parentheses is the LDAP display name for the attribute. The acceptable values for this parameter are:

  • A distinguished name
  • A GUID (objectGUID)
  • A security identifier (objectSid)
  • A SAM account name (sAMAccountName)

There is no “Given Name”. So you cannot use it. And it is because it is not necessarily unique accross your AD.

Objects have properties - just like in the real world. A chair has 4 legs, a seat and a backrest for example. So the chair is the object and “4 legs”, “a seat” and “a backrest” are the properties.

OK, but it is not possible to teach you a complex technology like a scripting language from scratch in a forum. You will have to learn the very basics by yourself first.

I didn’t get that question. sAMAccountName is the property of the “ADUser” object you just querried. When you use the pipeline the object itself is represented by the pipeline variable $_.

Please do a big step back and start with learning the fundamentals of PowerShell first. It will save you from a lot of frustrations and wasted time. And it will enable you to understand the help you are getting here. :wink:

1 Like

You changed your post after I started to answer …


Why don’t you google it at first? It doesn’t make that much sense when we google it for you. :wink: