r/PowerShell • u/Dirty_Panda715 • Dec 04 '25
Scripting Help
Hello everyone. I am trying to create a script that creates new ad users by using a csv file. I am struggling with the display name variable. I am setting display name has a variable. I have $DisplayName = “($user.’FIRST NAME’) + ($user.’LAST NAME’)”. Can someone help me figure out why it’s not working?
2
Upvotes
8
u/Quirky_Oil215 Dec 04 '25
How are you assigning the variable $user? What headers does your csv have ? Your concatenating the first name and last name together for the displayname
So John Smith is JohnSmith is that correct ?
Also as previous commentor has stated please share your code.
Trust me AI has stolen it already so dont be shy
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/import-csv?view=powershell-7.5
2
u/thisguyeric Dec 04 '25
What error are you getting? What is the code you're using to create the user?
You're doing variable expansion in strings wrong to start with, but without any details about what specifically is not working it's really difficult to provide any sort of help
7
u/AdeelAutomates Dec 04 '25 edited Dec 04 '25
Assuming your user fields are correct.
Since it's a variable in a single double quotes (string)...
To inject your variables in to that string directly with variables properties you need the extra dollar signs outside the bracket too. And you don't need to add plus between them.
Ie:
If you are going the concatenation route using plus, you write it like so:
I assume you are confusing the two.
7
u/BlackV Dec 04 '25 edited Dec 06 '25
Should be
But also likely you have 30 lines doing something like this
You could have a look at splatting
This makes your layout nicer and saves a million single use variables
This also ensures you get back a real ad object you can user later in your code, something like
Note it is good practice to specify a
-serverparameter on all your ad cmdlets so that you ensure that all the operators happen in 1 place and you're not fighting replicationP.s. who gave you that csv and has spaces on their bloody column names, they need a good box around the ears
Note: Did this on mobile, there maybe some errors
1
u/Ok_Mathematician6075 Dec 05 '25
Are you specifically having an issue with $ADUser.FirstName/LastName resolving?
1
u/[deleted] Dec 05 '25
I mean. We need your to put this through a foreach if it’s a list.
Then you can hinge on $user.firstname.
But if your list is part of $user. If you have more than one item. In the variable. It won’t work.
1
u/InfoZk37 Dec 06 '25
I don't have a displayname variable. When I set displayname I just use:
-DisplayName = $firstname $lastname
1
u/Training_Value5828 Dec 04 '25 edited Dec 04 '25
Do you have Microsoft Copilot? Search for: "Create a PowerShell script that creates new Active Directory users by using a csv file"
I'll never understand why, but people will flame you into the stone age if you actually post the code it writes - so I won't. (but you can message me, and I'll gladly share it with you)
The aforementioned search in Copilot will give you precisely what you're looking for, with error handling and verbose output to the console)
Ping me if you have questions.
-10
u/pigers1986 Dec 04 '25
maybe with string builder ...