I'm working on a PowerShell script to make it easier to create Active Directory users including some attributes. For now I need to process the "Manager" field. We have 4 departments, 4 managers. I want to create this using logic like "if department is 1 of the 4 departments then give 1 of the 4 managers."
How can I process this?
Import-Module activedirectory
#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv "c:\temp\scripts\ADUsers.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
$Firstname = $User.firstname
$Lastname = $User.lastname
$Username = $User.username
$Password = $User.password
$OU = $User.ou #This field refers to the OU the user account is to be created in
$userprincipalname = $user.userprincipalname
$displayname = $user.displayname
$title = $user.jobtitle
$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
{
Write-host
#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 -Name $displayname -SamAccountName $Username -UserPrincipalName $userprincipalname -GivenName $Firstname -Surname $Lastname -Enabled $True -DisplayName $displayname -AccountPassword (convertto-securestring $Password -AsPlainText -Force)
}
} #end function
Something like this?
"ManagerA/B/C/D" are SAM account name of user objects of managers.