AD Powershell Scripts – for New Active Directory user creation

ComputerCarriage > Posts > Active Directory > AD Powershell Scripts – for New Active Directory user creation

Hello All

We all know that there are many ways to create user accounts in Active directory like below :

  • Creating new users with Active Directory Administrative Center
  • Creating new with Active Directory Users and Computer
  • Creating new users with the dsadd command
  • Creating new users user powershell scripts (PS1)

But as a admin you all know powershell script will makes your work easy with less effort and zero error.

To help you in this part we have shared very simple powershell script below which helps you to creates a new active directory(AD) user account in single or bulk. 

and of course, there are plenty of scripts already available in internet but what I noticed and which makes me to write this post is most of the freely available scripts are just downloadable – that’s it, no reference or explanation which is quite hard for the system admin who has null/less experience with scripting to get modified based on their requirement. 

Here in this post I tried to give detailed insight in a simple way about the script for system admins who have less/null experience with scripting so that they can use it directly or alter this flexible script as per their requirement. 

Let’s jump.

Very first thing, to run this script you need active directory powershell module and privilege to create AD user object in your AD domain.

Admin has to type and save the new users details in the newusers.csv before running the script.

Below is the script on the on bold # statement I have given the explanation for the code for your better understanding. Please revert incase of any feedback or query.

AD Powershell Scripts

#Enter a path to your import CSV file
Import-Module ActiveDirectory
$ADUsers = Import-csv C:\scripts\newuser\newusers.csv # This is the file where we will input new users details

foreach ($User in $ADUsers) #For loop capture the details of new users feeded in the above CSV file and stores it in the respective variable
{
$OU = "<OU Path>" #Specify DN of the OU where you wish to create account
$Username = $User.username
$Password = $User.password
$FullName = $User.FullName
$Firstname = $User.firstname
$Lastname = $User.lastname
$DisplayName = $User.DisplayName
$Title = $user.Title
$Department = $User.department
$Manager = $User.Manager # provide manager's AD Samaccount Name in the csv file
$OfficePhone = $User.OfficePhone
$MobilePhone = $User.MobilePhone
$Company = "<Organization's Name>" # Specify organization name
$Country = "<Country Code>" # Specify county code e.g., US for USA, DE for Germany
$GroupName1 = "<Group Name>" # Specify Group name if you want to add user in the any of the group
$GroupName2 = "<Group Name>" # Specify Group name if you want to add user in the any of the group

#Check if the user account already exists in AD
if (Get-ADUser -F {SamAccountName -eq $Username})
{
#If user does exist, output a warning message
Write-Warning "A user account $Username has already exist in Active Directory."
}
else
{
#If a user does not exist then create a new user account

#Account will be created in the OU listed in the $OU variable in the CSV file; don’t forget to change the domain name in the"-UserPrincipalName" variable
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "$Username@abc.com" `
-Name $FullName `
-GivenName $Firstname `
-Surname $Lastname `
-DisplayName $DisplayName `
-Title $Title `
-Department $Department `
-Manager $Manager `
-OfficePhone $OfficePhone `
-MobilePhone $MobilePhone `
-Company $Company `
-City "Doha" `
-Country $Country `
-Enabled $True `
-ChangePasswordAtLogon $True `
-Path $OU `
-emailaddress "$username@abc.com" `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force) 

write-host $userName 'has been created' -foregroundcolor DarkGreen

#Adding newly created user to the security groups (if any)

Add-ADGroupMember -Identity $GroupName1 -Members $userName
write-host 'Account' $userName 'added' on $GroupName1 -foregroundcolor DarkGreen
Add-ADGroupMember -Identity $GroupName2 -Members $userName
write-host 'Account' $userName 'added' on $GroupName2 -foregroundcolor DarkGreen

}
}

And this is how  the CSV file will look like.

You can download the script & csv file template in the below Github link

GitHub

Refer below link to learn more AD powershell module cmdlets

https://docs.microsoft.com/en-us/powershell/module/addsadministration/?view=win10-ps

Home

AD Powershell Scripts

Leave a Reply