Solved

PowerShell New-AddUser

Posted on 2014-10-17
7
590 Views
Last Modified: 2014-10-23
Hey there,
I'm trying to add bulk users into Active Directory but for some reason it just won't take... The code is embedded here that I'm trying to run.

Import-Module ActiveDirectory
Import-Csv "C:\Scripts\test.csv" | ForEach-Object {
 $userPrincinpal = $_."samAccountName" + "@domainname.Local"
New-ADUser -Name $_.Name `
 -Path $_."Path" `
 -SamAccountName  $_."SamAccountName" `
 -UserPrincipalName  $userPrincinpal `
 -Surname $_. "Surname" `
 -Discription $_. "Discription" `
 -GivenName $_."GivenName" `
 -AccountPassword (ConvertTo-SecureString $_."AccountPassword" -AsPlainText -Force) `
 -ChangePasswordAtLogon $True `
 -Enabled $true
}

Open in new window


The error I get is:

PS C:\Users\Administrator.DomainName> C:\Scripts\Test.ps1
New-ADUser : A positional parameter cannot be found that accepts argument '.'.
At C:\Scripts\Test.ps1:4 char:1
+ New-ADUser -Name $_.Name `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Managemen
   t.Commands.NewADUser


I don't really understand why its giving me this error as the -Name parameter is valid i thought... I got the template for this code at

http://www.morgantechspace.com/2014/04/Create-Bulk-AD-Users-from-CSV-using-Powershell-Script.html 

and it seems to work for others. I tried putting in "-Name $_.Name `" and it gives the exact same error. I have attached the CSV file I'm referencing. It seems to have issues with the other ones that look like "-Name $_.Name `" if i remove the -Name attribute... It also doesn't enable the account even though the -Enable $_."Enable" ` is in there...
I have a huge list of people to add, The CSV is the same only this one doesn't have all the users in it. I can successfully add bulk users using my CSVs if i do this command in the PS window:

Import-Csv .|Test.csv | New ADUser

The problem I ran into there is AD wants the passwords as a secure string. I can add the attribute after the code above:

-AccountPassword (ConvertTo-SecureString "Test12344" -AsPlainText -Force)

And it works but I want the password from the CSV file. I know i could just add an attribute to make the user change the password but I want to be able to use the passwords from the CSV. That is why I want to use the Code that is attached.

Please excuse if some of my terms are wrong here as I am new to PS.
Testexample.csv
0
Comment
Question by:Jordan Smith
  • 4
  • 2
7 Comments
 
LVL 39

Expert Comment

by:footech
ID: 40387458
I think the error is from lines 8 and 9 where you have a space after the dot, also "description" was spelled wrong.  Should be:
 -Surname $_."Surname" `
 -Description $_."Description" `

Open in new window

The error is detected in the New-ADUser command, but doesn't get specific enough about where in the command.
0
 
LVL 10

Accepted Solution

by:
Michael Ian Claridge earned 500 total points
ID: 40387578
This is interesting as I've never used this method before.
I would normally use the below method as an example, its not complete but you get the idea.

$User is the element within the array of users which is referenced in this example by "$($user", the proceeding ".Attribute)" is the heading of the column heading. So if I wanted to reference the SamAccountName header, I would use $($User.SamAccountName).

I'm not saying that your method is wrong, and to be honest I have never even tried this method, this is just an alternative method that might be a little easier for you :)

$Users = Import-CSV -path "C:\Scripts\Test.csv"
foreach($User in $Users)
    {
        New-ADUser -SamAccountName $($User.SamAccountName) -Name $($User.Name) ETC ETC ETC ETC
    }

Open in new window


All the best.

Michael
0
 
LVL 39

Expert Comment

by:footech
ID: 40387607
@MichaelIanClaridge - It's just the difference between using the foreach statement vs. the ForEach-Object cmdlet.
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 10

Expert Comment

by:Michael Ian Claridge
ID: 40387618
@footech - Absolutely, I have just never used that method before to create AD Objects :)
The thing I love about PowerShell is that there is more than 1 way to achieve the same thing.

All the best.

Michael
0
 
LVL 39

Expert Comment

by:footech
ID: 40387649
I agree.  :)
0
 
LVL 1

Author Closing Comment

by:Jordan Smith
ID: 40400094
Thanks @MichaelIanClaridge! I ended up changing the script to your suggestion and it works great. This way is much simpler that the other way I was doing it... Thanks for the help!
0
 
LVL 39

Expert Comment

by:footech
ID: 40400189
I'm curious why you would think it's simpler?  About the only difference is that you're using $User instead of $_ for all the parameters.

Though to be honest, when I was first starting out in PowerShell I did find using the foreach statement easier to understand than the ForEach-Object cmdlet, I think mainly because I hadn't fully grasped the concept of the pipeline yet.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Set OWA language and time zone in Exchange for individuals, all users or per database.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring basic necessities in order to use the 2010 version of Data Protection Manager. These include storage, agents, and protection jobs. Launch Data Protection Manager from the deskt…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now