tech-man Posted September 4, 2015 Posted September 4, 2015 (edited) Hello I have taken a script from here and adapted it for what I think I need... but as a very newbie to Powershell it isn't working (surprise) and I expect I have done something wrong! Wonder if anyone would be kind enough to debug it for me! https://gist.github.com/979a588ae2085657e212 Thanks Edited September 4, 2015 by tech-man
tech-man Posted September 4, 2015 Author Posted September 4, 2015 Sorted - sorry - more like brain dead!
Steve21 Posted September 4, 2015 Posted September 4, 2015 Isn't line 1 supposed to be 2 lines? Import-Module ActiveDirectory $Users = Import-Csv -Delimiter "," -Path "c:\users\user\desktop\users.csv" Steve
tech-man Posted September 4, 2015 Author Posted September 4, 2015 ignore that, if i do .\create.ps1 - I get this... PS C:\users\user\Desktop> .\create.ps1 cmdlet New-ADUser at command pipeline position 1 Supply values for the following parameters: Name:
Steve21 Posted September 4, 2015 Posted September 4, 2015 You're lines can't be on different lines unless they're joined some how Eg new-aduser ` Name ` Etc etc Else it's taking the new user as a single command Steve
Arthur Posted September 5, 2015 Posted September 5, 2015 (edited) Wonder if anyone would be kind enough to debug it for me! Here's my attempt. When I tested it with your CSV file it worked perfectly. #Requires –Modules ActiveDirectory #Requires –Version 3 $Users = Import-Csv -Path "$env:USERPROFILE\Desktop\Users.csv" $Domain = (Get-ADDomain).DNSRoot $OU = (Get-ADDomain).UsersContainer ForEach ($User in $Users) { $Parameters = @{ Name = $User.Username SamAccountName = $User.Username UserPrincipalName = "$($User.Username)@$Domain" EmailAddress = $User.UPN.ToLower() DisplayName = "$($User.Username) - $($User.Firstname) $($User.Surname)" GivenName = $User.Firstname Surname = $User.Surname AccountPassword = (ConvertTo-SecureString -String $User.Password -AsPlainText -Force) Enabled = $true Description = $User.Description Path = $OU } New-ADUser @Parameters -PassThru } By using a hash table for the parameters the script is shorter and easier to read IMO. I assume the Name column in your CSV file is the display name for the user? If so, I thought it might be better to generate it dynamically through PowerShell rather than read it from the CSV since you could delete that column then. I added the Path parameter just in case you wanted to change the OU for the user accounts in the future. In the example above they will be created in the "Users" folder but you could change this to something else like "OU=Students,DC=domain,DC=com". Did you need to set anything else like the profile path, home drive and home directory? Btw, PowerShell v3.0+ will automatically import modules so you don't necessarily need "Import-Module ActiveDirectory". I added the first two lines so that the script doesn't run if the AD module isn't installed and PowerShell is older than v3.0. Edited September 5, 2015 by Arthur 3
halbaradkenafin Posted September 7, 2015 Posted September 7, 2015 You're lines can't be on different lines unless they're joined some how Eg new-aduser ` Name ` Etc etc Else it's taking the new user as a single command Steve This does work but if you accidentally add a space in after the "`" then it will fail as the "`" is an escape character. @Arthur's solution of using a hashtable is more reliable (and in my opinion more readable).
tech-man Posted September 7, 2015 Author Posted September 7, 2015 Thanks folks - worked a treat, understand it a bit more now!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now