Hello there,
I’ll start by pointing out that I am very new to PowerShell and to this forum.
So here is my situation:
I managed to find a script that created multiple users in AD based on a .csv file. Originally the .csv file included the following headers: CN,sAMAccountName,LastName,FirstName. I managed to add a Group header so that the created users are made members of groups based on what is on the .csv file.
It all works perfectly as I want it to. But now I want to add another header, “ParentOU” so that users will be created in or moved to the OU specified in the “ParentOU” column. That I was not able to include in my script and I am wondering if someone here could help me with that.
Sample .csv entry:
CN,sAMAccountName,LastName,FirstName,Group,ParentOU John Smith,SMIJ030787,Smith,John,Class01,'OU=Winter2015,OU=Students,DC=Domain,DC=Local'
Script:
$dataSource=import-csv -Path "C:\Users.csv" foreach($dataRecord in $datasource) { $cn=$dataRecord.cn $sAMAccountName=$dataRecord.sAMAccountName $givenName=$dataRecord.FirstName $sn=$dataRecord.LastName $group=$dataRecord.Group $ou=$dataRecord.ParentOU $displayName=$givenName + " " + $sn $userPrincipalName=$sAMAccountName + "@domain.local" $objUser=$objOU.Create("user","CN="+$cn) $objUser.Put("sAMAccountName",$sAMAccountName) $objUser.Put("userPrincipalName",$userPrincipalName) $objUser.Put("displayName",$displayName) $objUser.Put("givenName",$givenName) $objUser.Put("sn",$sn) $objUser.SetInfo() $objUser.SetPassword("SomePassword123") $objUser.Put("pwdLastSet",0) $objUser.psbase.InvokeSet("AccountDisabled",$false) $objUser.SetInfo() Add-ADGroupMember -Identity $group -Member $dataRecord.sAMAccountName }
As you can see from the script code, I defined a variable $ou for the OU in which the user is to be created at, but unfortunately, I don’t know how to continue with the proper command to make it work.
Thank you for all your help,