dQlle
dQlle

Reputation: 11

new-aduser and import-csv not working

I've got the following script which imports its data from a CSV file.:

    import-csv CSVfilelocation.csv | 
    new-aduser -name $_.Name -givenname $_.Firstname -surname $_.Lastname -samaccountname $_.samAccountName -userprincipalname ($_.samaccountname+"@dc.contoso.com") -manager $_.Manager -accountpassword (convertto-securestring Password -asplaintext -force) -path "Complete OU path" -Company $_.Company -Description $_.Description -enabled $false

The CSV file only contains two rows of data, The first row containing the column headers and the next row the data.

The script does not throw an error, but it doesn't do anything either. The user is not created and the scripts outputs the >> sign like it is waiting for input.

Executionpolicy is set to unrestricted and the script is run locally from the machine.

Why is it not working?

Upvotes: 0

Views: 774

Answers (3)

dQlle
dQlle

Reputation: 11

Found the problem, I had one " too many in my path variable. I guess I should proofread my code better next time.

Upvotes: 0

Vesper
Vesper

Reputation: 18747

Most likely you need an explicit foreach to call new-aduser several times.

import-csv CSVfilelocation.csv | % {
    new-aduser -name $_.Name -givenname $_.Firstname -surname $_.Lastname -samaccountname $_.samAccountName -userprincipalname ($_.samaccountname+"@dc.contoso.com") -manager $_.Manager -accountpassword (convertto-securestring Password -asplaintext -force) -path "Complete OU path" -Company $_.Company -Description $_.Description -enabled $false 
}

EDIT: I've missed that your Powershell waits for more input. This means you have an unclosed parentheses or string somewhere in your line, so check and fix.

Upvotes: 1

Glen J Fergo
Glen J Fergo

Reputation: 134

Please try this:

Use PowerShell to Read a CSV file and Create Active Directory User Accounts

http://blogs.technet.com/b/heyscriptingguy/archive/2011/12/22/use-powershell-to-read-a-csv-file-and-create-active-directory-user-accounts.aspx

* I THINK THE MOST IMPORTANT PART IS THAT YOU NEED TO STEP THROUGH EACH ITEM IN THE LIST. *

“So!” Lou popped in “We have your entire file in the Windows PowerShell variable $UserList. If we wish to step through this list, we simply use the ForEach-Object cmdlet. There are two ways that we can do this. One is to pipe the information directly into ForEach-Object like this:

$USERLIST | FOREACH-OBJECT { $_ }

“This will echo each entry in the list to the screen.”

“So what is that funny Windows PowerShell variable? The one with the underscore for a name?”

“AhHa! Glad you caught that!” piped up Boo. “That’s one of the many built in automatic variables that Windows PowerShell has. It contains the current object in the pipeline. As we stepped through the information in $UserList, this holds the entire content of that line. Or we can do it this way:

FOREACH ($Person in $UserList) { $ Person }

“In the second instance (using the $Person in $UserList setup), the variable $Person replaces the automatic variable, and it gives us something far more readable. Both work equally well, but I prefer the latter for readability. So if you’d like to access each entry in the list, we reference the names of the columns in your CSV file like this:”

FOREACH ($Person in $UserList) { 
    $Person.FirstName 
    $Person.LastName 
}

Hope that helps!!

Glen

Upvotes: 0

Related Questions