Skip to content
KBHost
Menu
KBHost
FileHost
Home
/
Knowledgebase
/
PowerShell & CMD
/
Snippets
/
New Active Directory User
New Active Directory User
# 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 } }
Random passwords
Foge3021!
Yaxo9721?
Baru6479!
Rofe9023!
Huni6736?
Most recent
Add entry to end of hosts file
Back-up TrueNAS config to Nextcloud
Nextcloud occ usage example
Add new user
Silent upgrade to Windows 10
New Active Directory User
Most viewed
Cleanup space in CertLog folder
Disable shared mailbox caching OST
Manual update FreeNAS
Ookla Speedtest server install
Flush DNS cache FRITZ!Box
Enable NFS client in Windows 10