Solved

Powershell User Import Script Server 2012 ADS

Posted on 2013-05-25
15
560 Views
Last Modified: 2014-04-03
Hi looking for a script to create a user account with the below info from a CSV can anyone help?

I have always done this with VBS but in 2012 it doesn't seem to work

sAMAccountName      
sn      
givenName      
displayName      
description      
password
0
Comment
Question by:barnyhall
  • 5
  • 5
15 Comments
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 39197251
$users = import-csv C:\..\Accounts.csv 
foreach ($user in $users) {
New-ADUser -Name $user.displayName -GivenName $user.givenName -Surname $user.sn `
-samAccountName $user.sAMAccountName -Enabled $true -Description $user.description `
-AccountPassword  (ConvertTo-SecureString $user.password -AsPlainText -force) -passthru
}

#oneliner
Import-Csv c:\..\accounts.csv | New-ADUser -Name $_.displayName -GivenName $_.givenName -Surname $_.sn -samAccountName $_.sAMAccountName -Enabled $true -Description $_.description -AccountPassword  (ConvertTo-SecureString $_.password -AsPlainText -force) -passthru

Open in new window

0
 

Author Comment

by:barnyhall
ID: 39197952
powershell just opens and closes with the above script  I have been trying with the below as well which dose seem to run but errors with

you cannot call a method on a null-valued expression

Import-Module ActiveDirectory
$Users = Import-Csv -Delimiter ";" -Path ".\2013.csv"  
foreach ($User in $Users)  
{  
    $OU = "OU=test,DC=internal,DC=site,DC=com"  
    $Password = $User.password
    $Detailedname = $User.firstname + " " + $User.name
    $UserFirstname = $User.Firstname
    $FirstLetterFirstname = $UserFirstname.substring(0,1)
    $SAM =  $FirstLetterFirstname + $User.name
    New-ADUser -Name $Detailedname -SamAccountName $SAM -UserPrincipalName $SAM -DisplayName $Detailedname -GivenName $user.firstname -Surname $user.name -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path $OU  
}
0
 
LVL 39

Expert Comment

by:footech
ID: 39198047
Typically the only reason that ve3ofa's wouldn't work would be because of the CSV - if its format is odd, or the headers don't match with the properties called in the script, or if you don't have headers.  Can you post a sample of the CSV?
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:barnyhall
ID: 39198052
sAMAccountName,sn,givenName,displayName,description,password
0
 
LVL 39

Expert Comment

by:footech
ID: 39198064
That's not a lot of help.  Is that the header row?  Or just an example of what each line contains?
An actual file would be much better with something like the first 3 lines.  You can put in some generic info if you want.  How is the file created?  I've seen issues where a BOM in the file messed up an import, but is not visible in Notepad and some other methods, hence an actual file is more useful.
0
 

Author Comment

by:barnyhall
ID: 39198078
Example attached thanks
2013.csv
0
 
LVL 39

Expert Comment

by:footech
ID: 39198128
Well, there's no problem with the CSV you provided.
Can you just run the New-ADUser command from the console and just fill in the info for each parameter instead of $user.property?  This would be a test trying to limit down where an error might be occurring.
0
 

Author Comment

by:barnyhall
ID: 39237300
can you send me a copy of the script that works for you please
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 39238520
All I did was adjust the script to call properties that matched the headers in the CSV you provided and it works.
Import-Module ActiveDirectory
$Users = Import-Csv -Path ".\2013.csv"  
foreach ($User in $Users)  
{  
    $OU = "OU=test,DC=internal,DC=site,DC=com" 
    $Password = $User.password 
    $Detailedname = $User.givenname + " " + $User.sn 
    $UserFirstname = $User.givenname 
    $FirstLetterFirstname = $User.givenname.substring(0,1) 
    $SAM =  $FirstLetterFirstname + $User.sn 
    New-ADUser -Name $Detailedname -SamAccountName $SAM -UserPrincipalName $SAM -DisplayName $Detailedname -GivenName $user.givenname -Surname $user.sn -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path $OU
}

Open in new window

0
 
LVL 39

Expert Comment

by:footech
ID: 39915130
If my last post with the script (http:#a39238520) helped you, or any other post, please accept those as solutions instead of your own post.
0
 

Author Comment

by:barnyhall
ID: 39922868
script worked thanks
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…

815 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

12 Experts available now in Live!

Get 1:1 Help Now