Description
Creating new Active Directory user is one of the regular task for every Administrator either for testing or for actual new employee. For that case you can create new AD user using ADUC console. But some of us feel it is time consuming job. To overcome this I have planned to write Powershell Script to Create new AD User. You can see different powershell script samples to create AD user in following examples.
You can use the Active Directory module’s cmdlet New-ADUser to create new AD user.
New-ADUser newUserName
Summary
Before start, ensure that the Active Directory module for Windows Powershell is installed or not by using following command. It will be installed by default in Domain Controller. In client machines, you need to install it through Remote Server Administration Tools.
Get-Module -Listavailable
If you are newbie to powershell, don’t forget to set your execution policy to unrestricted or you might get an error when you try run the script. Use the below command to set your execution policy:
Set-ExecutionPolicy Unrestricted
Create new Active Directory User using Powershell Script
1. Copy the below Powershell script and paste in Notepad file.
2. Change the Name, SamAccountName and DisplayName values into your own user name
3. Change the Parent OU path with your own OU’s DN
4. SaveAs the Notepad file with the extension .ps1 like Create-ADUser.ps1
Click to download Powershell script as file Download Create-ADUser.ps1
Import-Module ActiveDirectory New-ADUser ` -Name "TestUser" ` -Path "OU=TestOU,DC=TestDomain,DC=Local" ` -SamAccountName "TestUser" ` -DisplayName "Test User" ` -AccountPassword (ConvertTo-SecureString "MyPassword123" -AsPlainText -Force) ` -ChangePasswordAtLogon $true ` -Enabled $true Add-ADGroupMember "Domain Admins" "TestUser";
5. Now run the Create-ADUser.ps1 file in Powershell to create new Active Directory user using following command
PS C:Scripts> .Create-ADUser.ps1
Note: I have placed script file in the location C:Scripts, if you placed in any other location, you can navigate to that path using CD path command (like cd “C:Downloads“).
Create Bulk AD Users from CSV file using Powershell Script
1. Consider the CSV file NewUsers.csv which contains set of new ad users to create with the attributes Name, samAccountName and ParentOU.
Note: The value of ParentOU should be enclosed with double quote (“). like “OU=TestOU,DC=TestDomain,DC=Local” since it has the special character comma (,). because in csv file the comma (,) is the key character to split column headers. (Ex file: Download NewUsers.csv).
2. Copy the below Powershell script and paste in Notepad file.
3. Change the NewUsers.csv file path with your own csv file path.
4. Change the domain name TestDomain.local into your own domain name
5. SaveAs the Notepad file with the extension .ps1 like Create-BulkADUsers-CSV.ps1
Click to download Powershell script as file Download Create-BulkADUsers-CSV.ps1
Import-Module ActiveDirectory Import-Csv "C:\ScriptsNewUsers.csv" | ForEach-Object { $userPrincinpal = $_."samAccountName" + "@TestDomain.Local" New-ADUser -Name $_.Name ` -Path $_."ParentOU" ` -SamAccountName $_."samAccountName" ` -UserPrincipalName $userPrincinpal ` -AccountPassword (ConvertTo-SecureString "MyPassword123" -AsPlainText -Force) ` -ChangePasswordAtLogon $true ` -Enabled $true Add-ADGroupMember "Domain Admins" $_."samAccountName"; }
6. Now run the Create-BulkADUsers-CSV.ps1 file in Powershell to create Bulk Active Directory users from CSV file.
PS C:Scripts> .Create-BulkADUsers-CSV.ps1
Note: I have placed script file in the location C:Scripts, if you placed in any other location, you can navigate to that path using CD path command (like cd “C:\Downloads”).
7. Now you can check the newly created AD users though ADUC console.
Powershell Script to Create Bulk AD Users for Testing
1. Copy the below Powershell script and paste in Notepad file.
2. Change the value for the variable $totalUsers as per your wish like 1000 or more
3. Change the Parent OU path with your own Test OU’s DN
4. SaveAs the Notepad file with the extension .ps1 like Create-BulkADUsers.ps1
Click to download Powershell script as file Download Create-BulkADUsers.ps1
Import-Module ActiveDirectory $totalusers = 10 for ($i=0; $i -lt $totalusers; $i++) { $userID = "{0:00}" -f ($i + 1) $userName = "TestUser$userID" Write-Host "Creating AD user" ($i + 1) "of" $totalusers ":" $userName New-ADUser ` -Name $userName ` -Path "OU=TestOU,DC=TestDomain,DC=Local" ` -SamAccountName $userName ` -AccountPassword (ConvertTo-SecureString "MyPassword123" -AsPlainText -Force) ` -Enabled $true Add-ADGroupMember "Domain Admins" $userName; }
5. Now run the Create-BulkADUsers.ps1 file in Powershell to create Multiple Active Directory users for Testing.
PS C:Scripts> .Create-BulkADUsers.ps1
The term '-Path' is not recognized as the name of a cmdlet
Worked like a charm. Remember to run powershell as admin. Thank You Morgan you Rock!