Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###################################################################
- #Author : oneZergArmy
- #Date : 14.08.2015
- #Version : 1.1
- #Desc. : Create users from .csv file and add them to a group
- ###################################################################
- Import-Module ActiveDirectory
- #Imports the .csv file. Remember to change the path and name of the file.
- $csv = import-csv -Path 'C:\Students-ICT.csv' -Delimiter ';'
- #Goes through each row in the .csv file.
- foreach ($row in $csv)
- {
- #------------------------------------------------------------------
- #VARIABLES - Change these
- #------------------------------------------------------------------
- #Domain name for the server
- $domain = '@lab.local' #testing server
- #Default password
- $password = "Passord1"
- #Paths for were the users are added
- $path = "ou=$row.Klasse,ou=Brukere,ou=IKT-FAG,dc=lab,dc=local"
- #------------------------------------------------------------------
- #VARIABLES - Don't change
- #------------------------------------------------------------------
- #Splits the given name and the surname into seperate variables.
- $givenName = $row.Name.split()[0]
- $surname = $row.Name.split()[1]
- #Gets the users class from the csv document
- $class = $row.Klasse
- #Generates a username. Jake Doe = JakDoe
- $username = $givenName.substring(0,3) + $surname.substring(0,3)
- #UserPrincipalName generator
- $UPN = $username + $domain
- #Checks if the username already exists in AD
- $checkUser = Get-ADUser -filter {SamAccountName -eq $username}
- #------------------------------------------------------------------
- #Script
- #------------------------------------------------------------------
- #Function for creating a user
- Function createUser(){
- New-ADUser -Name $row.Name -GivenName $givenName -Surname $surname -displayName $row.Name -AccountPassword (ConvertTo-SecureString -AsPlainText $password -Force) -ChangePasswordAtLogon $true -SamAccountName $username -UserPrincipalName $UPN -Path $path -Enabled $true
- }
- #Checks if the user is already in AD. If so, the user will be named something like JakDoe2
- if($checkUser){
- $username = $username + 2
- createUser
- #If it does not exist, user will be created and named JakDoe
- }else {
- createUser
- }
- #Add the user to the correct group
- Add-ADGroupMember -Identity $class -Member $username
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement