TechShizz | Creating users in Bulk with PowerShell

Creating users in Bulk with PowerShell

Finding Commands

 

Get-command *AD*

 

New-ADUser

Remove-ADUser

 

New-ADUser -Path "ou=User Accounts,dc=contoso,dc=com" -Name "Mary North"

-SAMAccountName "mary.north" -UserPrincipalName "mary.north@contoso.com"

-EmailAddress "mary.north@contoso.com" -GivenName "Mary" -Surname "North"

-Description "Sales Representative in Australia"

-Company "Contoso, Ltd." -Department "Sales"

-Office "Sydney"

 

Password Set

 

-AccountPassword (ConvertTo-SecureString -AsPlainText "Pa$$w0rd" -Force)

-ChangePasswordAtLogon $true -Enabled $true

 

Piped command

 

Get-ADUser "mary.north" | Set-ADUser -DisplayName "North, Mary"

 

Variable Command

 

$user = Get-ADUser "mary.north"

Set-ADUser $user -EmployeeNumber 12345

 

 

IMPORT FROM CSV

 

$UserList=IMPORT-CSV c:\users\administrator\documents\newusers.csv

 

# Step through Each Item in the List

 

FOREACH ($Person in $UserList) {

 

# Build Username

 

$Username=$Person.Username

 

# Build Password from Firstname and Lastname

 

$Password=$Person.Firstname+$Person.Lastname

 

# Build the Displayname

 

$Name=$Person.Firstname+” “+$Person.Lastname

 

# Build and define Domain name

 

$Domain="@teamrou.com"

 

# Build User Principal Name

 

$UPN=$Username+$Domain

 

# Build and define Home Directory path

 

$HDrive="\\Shares\%username%\"

 

# Build and define which Organizational Unit to create User inside

 

$OU="OU=test,DC=yourdomainhere,DC=com"

 

# Create Account in Active Directory (AND HERE...WE...GO!)

 

New-ADUser -Name $Name –GivenName $Person.Firstname –Surname $Person.Lastname –DisplayName $Name –SamAccountName $Username -HomeDrive "H:" -HomeDirectory $HDrive –UserPrincipalName $UPN -Path $OU

 

# Set Password

 

Set-ADAccountPassword -Identity $Username -NewPassword (ConvertTo-SecureString -AsPlainText $Password -Force)

 

# Add User to Security Groups

Add-ADPrincipalGroupMembership -Identity $Username -MemberOf "Sales","Test"

 

# Enable Account

Enable-ADAccount -Identity $Username

}

 

 

Add comment