Skip to content
KBHost
Menu
216.73.216.170
KBHost
FileHost
Home
/
Knowledgebase
/
PowerShell & cmd
/
Snippets
/
New Active Directory User
New Active Directory User
0
0
# Elevate to Admin privileges param([switch]$Elevated) function Check-Admin { $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) } if ((Check-Admin) -eq $false) { if ($elevated) { # could not elevate, quit } else { Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition)) } exit } # Import active directory module for running AD cmdlets Import-Module activedirectory #Store the data from ADUsers.csv in the $ADUsers variable $ADUsers = Import-csv C:\Users\admin\Desktop\New-ADUser\users.csv #Loop through each row containing user details in the CSV file foreach ($User in $ADUsers) { #Read user data from each field in each row and assign the data to a variable as below $Username = $User.username $Password = $User.password $Firstname = $User.firstname $Lastname = $User.lastname $OU = $User.ou #This field refers to the OU the user account is to be created in $email = $User.email $streetaddress = $User.streetaddress $city = $User.city $zipcode = $User.zipcode $state = $User.state $country = $User.country $telephone = $User.telephone $jobtitle = $User.jobtitle $company = $User.company $department = $User.department #Check to see if the user already exists in AD if (Get-ADUser -F {SamAccountName -eq $Username}) { #If user does exist, give a warning Write-Warning "A user account with username $Username already exist in Active Directory." } else { #User does not exist then proceed to create the new user account #Account will be created in the OU provided by the $OU variable read from the CSV file New-ADUser ` -SamAccountName $Username ` -UserPrincipalName "$Username@domain.com" ` -Name "$Firstname $Lastname" ` -GivenName $Firstname ` -Surname $Lastname ` -Enabled $True ` -DisplayName "$Firstname $Lastname" ` -Path "OU=MailUsers,OU=Company Name,DC=DOMAIN,DC=local" ` -City $city ` -Company $company ` -State $state ` -StreetAddress $streetaddress ` -OfficePhone $telephone ` -EmailAddress "$Username@domain.com" ` -Title $jobtitle ` -Department $department ` -OtherAttributes @{proxyAddresses = ("SMTP:" + "$Username@domain.com"), ("smtp:" + "$Username@seconddomain.com")} ` -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $False } }
No sleep -
00:00
Password Generator
Most viewed
Connect to Office 365 with PowerShell
Silent update Microsoft Remote Desktop
Add Microsoft Photos app to Server 2019
Enable NFS client in Windows 10
Clear Outlook filter for all folders
Asus ZenWiFi LED color status
Ventoy Windows 11 To Go
Find and delete all snapshots
Most recent
Install or Update reMarkable
TrueNAS Clear failed jobs history
TrueNAS SSH Hetzner Storage Box Rsync
Print Screen from PowerShell
Create local user during Windows Setup
Force Microsoft 365 installation
Enable MailTips in Microsoft 365
Get Home Assistant Logs
We use cookies to ensure the best experience on our website. If you continue to use this site, we assume that you are happy with this.
Ok