Creating AD Users with PowerShell
Instructions
Q&A (0)
Notes (0)
Resources (2)
Saving Progress...
Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.
In this lecture, I am going to be showing you how to create Active Directory Users with PowerShell.We are working on IPDC01. Remember that we need to execute these scripts in a Domain Controller with Active Directory or a computer that has the RSAT tools connected to a server that has the Active Directory Domain Services installed.
Open Windows PowerShell ISE from the start menu.
Windows PowerShell ISE will be launched. We prefer PowerShell ISE since it helps in developing and creating scripts and also if needed we can type commands in the button pane window.
Let’s start by typing the Comment and Import the Active Directory Module.Since we are creating AD Users, we can type get-help New-ADUser to see all the options we can use.
We will be typing our commands in separate lines using the backtick or grave accent (next to number 1) to have one long script break down in separate lines.
# Import AD module
Import-Module ActiveDirectory
# Create the AD User
New-ADUser `
-Name "Bradley Beal" `
-GivenName "Bradley" `
-Surname "Beal" `
-UserPrincipalName "Bradley.Beal" `
-AccountPassword (ConvertTo-SecureString "P@$$w0rd123" -AsPlainText -Force) `
-Path "OU=Domain Users,OU=instructorpaul,DC=instructorpaul,DC=com" `
-ChangePAsswordAtLogon 1 `
-Enabled 1
Now click on the green Play icon to execute the script.
We can see the script executed.Check Active Directory to see if it is created (you might need to click refresh). We see our user account in Active Directory.
Now let’s delete the user account by right-clicking on the user and selecting Delete.
Click Yes to confirm.
Now that the script worked we can work on it. We can modify it to have the user enter their first and last name and create the user account.
First, we will grab some variables. Let’s see how the script looks now.
# Import AD module
Import-Module ActiveDirectory
# Grab variables from user
$firstname = Read-Host -Prompt "Please enter the first name"
$lastname = Read-Host -Prompt "Please enter the last name"
# Create the AD User
New-ADUser `
-Name "$firstname $lastname" `
-GivenName $firstname `
-Surname $lastname `
-UserPrincipalName "$firstname.$lastname" `
-AccountPassword (ConvertTo-SecureString "P@$$w0rd123" -AsPlainText -Force) `
-Path "OU=Domain Users,OU=instructorpaul,DC=instructorpaul,DC=com" `
-ChangePAsswordAtLogon 1 `
-Enabled 0
Execute the script by clicking on the Play button.We see the script executing and asking for the First and Last name of the user.
If we check Active Directory we can see the newly created user.
We can run it again and create another user, that is why creating users in PowerShell might be a good idea for you.Now that we have the script we can save it by clicking File > Save As...
Save it to the Desktop and name it CreateADUser and click on Save.
Now it is easy to execute the saved script. Right-click and choose Run with PowerShell.
We will just need to type the first name and last name and the user account will be created.
We can confirm by checking Active Directory Users and Computers.
And that’s how we create AD Users with PowerShell.
Note: For the attached script to work, you MUST change the target OU path
Hi, I created my own Virtual Network with VirtualBox, created the Script as you described in this Lecture, but although the User Account comes up in dsa.msc, I’m unable to log in with the Credentials, i.e. firstname.lastname & P@$$w0rd123 ? I’m attaching my Script again below as an attachment, because it didn’t come up legible when I sent it before:
Hi Yitzchok Rooz,
The script seems to be about right (will need to test in a lab). If the script executes with no error and you see the users, are you login to the same server? or what message do you get?
Ricardo
The Error I’m getting when signing in from a Windows 10 Pro Virtual Machine on the same Network / Domain is:
The user name or password is incorrect. Try again.
I verified that the username and password ARE correct?
Thank you. Let me check on the lab to see why is it not working.
From my testing what I can see is the issue might be with the password.
I have the same issue as you are having.
In order to test I reset the password from AD by right clicking the user and reset password.
Then I test from windows 10 and I get the new message relating to resetting the user password at first login.
If you can test doing the reset to see if it works. I will check why is it the issue for resetting password.