I wrote this as I could not find any example script to do this and would like to share it with others. ----------------
foreach ( $record in (Import-Csv e:\scripts\users.csv)) {
$command = "New-ADUser "
$DefaultGroup = $record.DefaultGroup
$name = $record.SamAccountName
foreach ( $attribute in
(Get-Member -InputObject $record -MemberType NoteProperty) ) {
$value = $record.($attribute.Name)
if (($value) -and ($attribute.Name -cne "DefaultGroup")) {
$command += " -$($attribute.Name) '$value'"
}
}
$common = " -Enabled 1 –ChangePasswordAtLogon 1 -AccountPassword (ConvertTo-SecureString 'p@ssw2010' -AsPlainText -force)"
$command+=$common
#Write-Host $command
Invoke-Expression $command
Add-ADGroupMember $DefaultGroup $name } ----------------- users.csv -----------------
Name,GivenName,Surname,DisplayName,SamAccountName,UserPrincipalName,Description,Department,Path,HomeDrive,HomeDirectory,DefaultGroup
Jennifer Wong,Jennifer,Wong,Jennifer Wong,JWong,JWong@bglcorp.local,Wiki Editor,Support,"OU=Users,OU=TEST,DC=corp,DC=local","R:","\\dc1\home\JWong",Support -----------------
Also does anyone know of a method where I can add the other attributes from the csv and one additional common method for example import-csv c:\users.csv | New-AdUser $_, -Enabled 1 this gives me an error on $_ but the new-aduser already uses the for-each object so I am confused here ?
|