Jump to content

Recommended Posts

Posted

Isn't line 1 supposed to be 2 lines?

 

Import-Module ActiveDirectory

$Users = Import-Csv -Delimiter "," -Path "c:\users\user\desktop\users.csv"

 

Steve

Posted

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:

Posted

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

Posted (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 by Arthur
  • Thanks 3
Posted
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).

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...