hello all i have a new client that has a ton of users… they werent using active directory. and im going crazy typing these users in and i was like… it would be great to import all of these users!<\/p>\n
is there a way to script a csv file to import these headers into active directory on server 2012 r2?<\/p>\n
Username
\nfirst name
\nlast name
\ntemp password
\nfull name
\nOU they belong infor group policy
\nBranch name
\nEmail<\/p>\n
please advise. thanks<\/p>","upvoteCount":6,"answerCount":6,"datePublished":"2019-03-05T13:51:22.000Z","author":{"@type":"Person","name":"deanbossert4","url":"https://community.spiceworks.com/u/deanbossert4"},"acceptedAnswer":{"@type":"Answer","text":"
AS per your DM you need<\/p>\n
username
\nfirstname
\nlastname
\npassword
\nFull name
\nOU
\nemail<\/p>\n
add those as a header in CSV and run<\/p>\n
If (!(Get-module ActiveDirectory )) {\n Import-Module ActiveDirectory\n Clear-Host\n }\n\n$Users=Import-csv c:\\users.csv\n$a=1;\n$b=1;\n$failedUsers = @()\n$successUsers = @()\n$VerbosePreference = \"Continue\"\n$ErrorActionPreference='stop'\n$LogFolder = \"$env:userprofile\\desktop\\logs\"\n\n ForEach($User in $Users)\n {\n $FirstName = $User.FirstName.substring(0,1).toupper()+$User.FirstName.substring(1).tolower()\n $LastName = $User.LastName.substring(0,1).toupper()+$User.LastName.substring(1).tolower()\n\n $FullName = $User.FirstName + \" \" + $User.LastName\n\n $SAM = $user.username\n <#\n $Sam=$User.FirstName+$User.LastName --> example john snow will be Johnsnow\n $Sam=$User.FirstName --> example john snow will be John\n $Sam= $User.firstName + \".\" + $User.lastName --> example john snow will be John.snow\n $Sam=$user.Lastname+$user.Firstname.Substring(0,1)) --> example john snow will be sjohn\n #>\n\n $dnsroot = '@' + (Get-ADDomain).dnsroot\n\n $SAM=$sam.tolower()\n\n # To set Diffreent Passwords for each User add header Password on CSV and change 'P@ssw0rd@123' to $user.passsword\n $Password = (ConvertTo-SecureString -AsPlainText $user.password -Force)\n\n \n $UPN = $SAM + \"$dnsroot\" # change \"$dnsroot to custom domain if you want, by default it will take from DNS ROOT\"\n\n $OU=$user.OU\n $email=$user.email\nTry {\n if (!(get-aduser -Filter {samaccountname -eq \"$SAM\"})){\n $Parameters = @{\n 'SamAccountName' = $Sam\n 'UserPrincipalName' = $UPN \n 'Name' = $Fullname\n 'EmailAddress' = $Email \n 'GivenName' = $FirstName \n 'Surname' = $Lastname \n 'AccountPassword' = $password \n 'ChangePasswordAtLogon' = $true # Set False if you do not want user to change password at next logon.\n 'Enabled' = $true \n 'Path' = $OU\n 'PasswordNeverExpires' = $False # Set True if Password should expire as set on GPO.\n}\n\nNew-ADUser @Parameters\n Write-Verbose \"[PASS] Created $FullName \"\n $successUsers += $FullName + \",\" +$SAM\n }\n \n}\nCatch {\n Write-Warning \"[ERROR]Can't create user [$($FullName)] : $_\"\n $failedUsers += $FullName + \",\" +$SAM + \",\" +$_\n}\n}\nif ( !(test-path $LogFolder)) {\n Write-Verbose \"Folder [$($LogFolder)] does not exist, creating\"\n new-item $LogFolder -type directory -Force \n}\n\nWrite-verbose \"Writing logs\"\n$failedUsers |ForEach-Object {\"$($b).) $($_)\"; $b++} | out-file -FilePath $LogFolder\\FailedUsers.log -Force -Verbose\n$successUsers | ForEach-Object {\"$($a).) $($_)\"; $a++} | out-file -FilePath $LogFolder\\successUsers.log -Force -Verbose\n\n$su=(Get-Content \"$LogFolder\\successUsers.log\").count\n$fu=(Get-Content \"$LogFolder\\FailedUsers.log\").count\n\nWrite-Host \"$fu Users Creation Failed and \" -NoNewline -ForegroundColor red\nWrite-Host \"$su Users Successfully Created \" -NoNewline -ForegroundColor green\nWrite-Host \"--> Launching LogsFolder have a Look and review.\" -ForegroundColor Magenta\nStart-Sleep -Seconds 5\nInvoke-Item $LogFolder\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2019-03-06T07:41:31.000Z","url":"https://community.spiceworks.com/t/importing-users-to-active-directory/700545/6","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},"suggestedAnswer":[{"@type":"Answer","text":"hello all i have a new client that has a ton of users… they werent using active directory. and im going crazy typing these users in and i was like… it would be great to import all of these users!<\/p>\n
is there a way to script a csv file to import these headers into active directory on server 2012 r2?<\/p>\n
Username
\nfirst name
\nlast name
\ntemp password
\nfull name
\nOU they belong infor group policy
\nBranch name
\nEmail<\/p>\n
please advise. thanks<\/p>","upvoteCount":6,"datePublished":"2019-03-05T13:51:22.000Z","url":"https://community.spiceworks.com/t/importing-users-to-active-directory/700545/1","author":{"@type":"Person","name":"deanbossert4","url":"https://community.spiceworks.com/u/deanbossert4"}},{"@type":"Answer","text":"
It can be done with a powershell script<\/p>\n