Set AD OU based on CSV Column

I’m hoping to get some help setting a variable within a foreach loop using import-csv. The part I’m having issues with is setting the OU path inside the foreach loop. I get the following message when I run the script:

cmdlet New-ADUser at command pipeline position 1 Supply values for the following parameters: Name: 

The column I’m trying to use to determine the OU is:


The script is below, sorry for the n00bishness. Thank you for any help that can be provided. 🙂

Import-Module ActiveDirectory # Get the path $path = Split-Path -parent $MyInvocation.MyCommand.Definition $stud_accounts_path = $path + "ad-new-stu.csv" Import-CSV $stud_accounts_path | foreach { # Set OU in AD $ou = "OU=$_.gradClass,OU=Students,DC=domain,DC=local" New-ADUser ` -SamAccountName $_.sAMAccountName ` -UserPrincipalName $_.UserPrincipalName ` -GivenName $_.GivenName ` -Surname $_.Surname ` -Name $_.DisplayName ` -EmailAddress $_.Mail ` -Title $_.Title ` -Description $_.Description ` -Department $_.Department ` -Company $_.Company ` -AccountPassword (ConvertTo-SecureString -AsPlainText $_.AccountPassword -Force) ` -Path $ou ` -Enabled $true ` -ChangePasswordAtLogon $false ` -PasswordNeverExpires $true } 

submitted by /u/dasunsrule32
[link] [comments]

Leave a Reply