Skip to content

Instantly share code, notes, and snippets.

@rpc180
Last active September 17, 2020 13:59
Show Gist options
  • Save rpc180/20df7caca75b8146ecee01f677fe755b to your computer and use it in GitHub Desktop.
Save rpc180/20df7caca75b8146ecee01f677fe755b to your computer and use it in GitHub Desktop.
Active Directory Object Creation Scripts
#
# Scripts to create the AD OUs, Groups, and users
# Accounts are created in a disabled state so that the password issue during bulk creation is not a problem, reset passwords and enable accounts as needed
# Create OUs first, Create Domain Groups Second, Create Users Last (User creation will autopopulate groups)
#
# Ensure network mapping to source .csv files prior to running scripts, and recommended to run scripts from powershell ISE interface
#
# CREATE OU STRUCTURE
$offices = Import-Csv v:\PopulateAD_Groups_Users\officeOUs.csv
# Example Offices CSV Lines (OUs)
# orgunit,path
# Domain Member Servers,"DC=mydomain,DC=com"
# Los Angeles,"OU=Domain Users,DC=mydomain,DC=com"
# Arlington,"OU=Domain Users,DC=mydomain,DC=com"
foreach ( $office in $offices) {
New-ADOrganizationalUnit -Name $office.orgunit -ProtectedFromAccidentalDeletion $false -path $office.path
}
#CREATE DEPARTMENT GROUPS (Security Groups)
$departments = Import-Csv v:\PopulateAD_Groups_Users\DepartmentOUs.csv
# Example Departments CSV Lines
# department,GroupScope,Path,GroupCategory
# Management,Global,"OU=Domain Groups,DC=mydomaint,DC=com",Security
# IT,Global,"OU=Domain Groups,DC=mydomain,DC=com",Security
foreach ( $department in $departments ) {
New-adgroup -Name $department.department -GroupScope Global -GroupCategory $department.groupcategory -path $department.path
}
#CREATE NEW USERS (With Group Memberships)
$users = Import-Csv v:\PopulateAD_Groups_Users\Users.csv
# Example Users CSV Line:
# employeenumber,surname,givenname,office,department,city,state,samaccountname,emailaddress,name,path,enabled
# 20162400,Gonzales,Dennis,Los Angeles,Management,Los Angeles,CA,Dennis.Gonzales,[email protected],"Gonzales, Dennis","OU=Los Angeles,OU=Domain Users,DC=mydomain,DC=com",FALSE
foreach ( $obj in $users ) {
# remove-aduser -identity $obj.samaccountname }
$newperson = new-aduser -passthru -Name $obj.name -UserPrincipalName $obj.emailaddress -Surname $obj.surname -Givenname $obj.givenname -employeenumber $obj.employeenumber -office $obj.office -city $obj.city -state $obj.state -samaccountname $obj.samaccountname -EmailAddress $obj.emailaddress -displayname $obj.name -path $obj.path -department $obj.department -enabled $false
add-adgroupmember $obj.department $newperson
}
# Uncomment line 46 to remove destroy any user objects included in the user.csv file if there was a mistake.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment