How to create a new user account with Powershell
This powershell script is helpful for anyone who creates new active directory user accounts multiple times a week and would like to reduce redundancy and time in that process. Back in the day I worked in a company with a high retention rate and would have to create and retire active directory user accounts on the fly. The script came in handy when HR would forget to inform me, a week ago they added five new hires and they’re about to start in 20 minutes. Great I thought. That was the inspiration behind this script. So boys and girls this is how to create a new user account with Powershell.
Download this powershell script on Github.
How It Works
The script first begins with Read-Host asking you to input the new users first name, last name, and stores them in a variable.
Then the $recommendedUser variable take the first letter of the first name, the complete last name, and merges them together. So John Smith’s username would be Jsmith. Feel free to modify this section since I know different companies have different rules for usernames.
Then I set Password1 as the default password in $password when creating the username. In the future I set the password to change at login, since password is a no-no.
While working on this script I ran into issue where the script would fail if I accidentally created an account with an already existing samaccountname. So in the try statement, I added a check to see if a user with an existing samaccount name already exist. If it doesn’t, then perfect it’ll continue down the line. But if the username already existed, I added a variable called $newUsername which basically just ask you to manually create your own username. So Jsmith can now be Jsmith1.
Finally we get to the action. We run New-AdUser with all the new user information, enable the account, and set the password to change at logon.
-Make sure to set the correct OU path where you’d like the new account to go. If we wanted to, we could make the script more complex by adding company phone number, department, OU paths, etc. But we’ll leave that for another day.
Feel free to leave a comment with any suggestions or questions.
Interested In learning Powershell?
Check out “PowerShell in Depth“. PowerShell in Depth, Second Edition is the go-to reference for administrators working with Windows PowerShell. Every major technique, technology, and tactic is carefully explained and demonstrated, providing a hands-on guide to almost everything an admin would do in the shell. Written by three experienced authors and PowerShell MVPs, this is the PowerShell book you’ll keep next to your monitor—not on your bookshelf!
- Automating tasks
- Packaging and deploying scripts
- Introduction to Desired State Configuration
- PowerShell security
- Covers PowerShell version 3 and later
This post “Create A New User Account with Powershell” is located under the Programming Category
Learn how to create a Build a dynamic menu written in Powershell!