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.
IMO, the piece that isn't working is the -Path variable you're supplying. You need to provide the DN (distinguishedName) of a path in active directory. Simplest way to do this is grab the DN of a user and get the parent container DN.
Something like this:
-Path 'OU=New User Accounts,OU=Users,DC=compost,DC=is,DC=smelly,DC=com'
Also, I'd advise you to get used to delimiting strings the same way every time and stick with it. Single and double quotes act differently. You don't have to delimit field names from your source .csv unless there are white spaces.
If you're using separate containers, just construct the parent path of the new user object on they fly.
$OU
A working example of that might be:
Start
End
($Creation_PW should be a SecureString)
I pulled sections from a mass account creation script that I use for my organization. We do them on occasion. You can specify all sorts of attributes upon creation, depending upon your AD schema. I've left only the relevant pieces.
Hopefully, this helps