My aim is to import users in a CSV file to Active Directory via a simple PowerShell Script. Despite this I'm encountering a syntax error as seen below.
Updated: CSV Column Format
name,surname,ou Steven,Boone,Management Rodney,Fisher,Sales Taylor,Bautista,Management Nathan,Morris,Management
Working and Solved: PowerShell Code
Import-Module ActiveDirectory
$ADDSUsers = Import-Csv C:\0469697M_gxt.csv
foreach ($user in $ADDSUsers) {
$Name = $user.name + " " + $user.surname
$OU = $user.ou
$OUPath = "OU=$($OU),dc=intgxt,dc=allaboutfood,dc=com,dc=mt"
#Creating New AD Users
New-ADUser -Name $Name -Path $OUPath
}
The error
New-ADUser : The object name has bad syntax
At C:\Script.ps1:9 char:5
+ New-ADUser -Name "$name" -Path "$OU"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (CN=Tyler Blair,Management:String) [New-ADUser], ADException
+ FullyQualifiedErrorId :ActiveDirectoryServer:8335,Microsoft.ActiveDirectory.Management.Commands.NewADUser
Not sure where I have an error. As far as I'm concerned the columns are being parsed well.
NotSpecified: (CN=Tyler Blair,Management:String)says to me that the CSV parser is falling over on the OU data, which should be likeCN=Tyler Blair,OU=This,DC=example,DC=comand your CSV doesn't have that quoted, so Import-CSV is splitting the OU up on commas as if they were different columns, and then you're creating an object without a full path and it's failing on that. A guess, because your code doesn't look incorrect. Alternately maybe the OU in your spreadsheet isn't a full path and needs to be