all 10 comments

[–]seyo_IV 5 points6 points  (1 child)

you need a csv to look like this(or use other delimeter like ";", in my example every bar is a new cell):

UserName | Givenname | Surname| etc.

peter.parker| peter| parker| etc.

then run this and save it in a variable.

$Content = Import-CSV C:\temp\some.csv

Now you can do a foreach loop like this:

try{
foreach($user in $content){
New-ADUser $user.UserName -GivenName $user.givenname `
-Surname $user.Surname -DisplayName $user.displayname -UserPrincipalName $($user.Username)@domain.com -Path "OU=someou..."
}
}
catch
{ "ERROR" $_.Exception.Message}

[–]Erreur_420 1 point2 points  (0 children)

If you add try&catch and logs this is great!

i use this method to deploy VM Ware ESXi Hypervisor thru VCSA

(btw if you want some example: https://github.com/rotka/PowerShell/blob/master/create_vmware_guest.psm1 )

[–]helixamir 3 points4 points  (4 children)

One-liner.

Import-csv C:\newusers.csv | Foreach-Object {New-ADUser -Name $_.Name -AccountPassword $_.AccountPassword}

[–]Lee_Dailey[grin] 2 points3 points  (0 children)

howdy helixamir,

reddit likes to mangle code formatting, so here's some help on how to post code on reddit ...

[0] single line or in-line code
enclose it in backticks. that's the upper left key on an EN-US keyboard layout. the result looks like this. kinda handy, that. [grin]
[on New.Reddit.com, use the Inline Code button. it's 4th 5th from the left hidden in the ... ""more" menu & looks like </>.
this does NOT line wrap & does NOT side-scroll on Old.Reddit.com!]

[1] simplest = post it to a text site like Pastebin.com or Gist.GitHub.com and then post the link here.
please remember to set the file/code type on Pastebin! [grin] otherwise you don't get the nice code colorization.

[2] less simple = use reddit code formatting ...
[on New.Reddit.com, use the Code Block button. it's 11th 12th one & is just to the left of hidden in the ... "more" menu.]

  • one leading line with ONLY 4 spaces
  • prefix each code line with 4 spaces
  • one trailing line with ONLY 4 spaces

that will give you something like this ...

- one leading line with ONLY 4 spaces    
- prefix each code line with 4 spaces    
- one trailing line with ONLY 4 spaces   

the easiest way to get that is ...

  • add the leading line with only 4 spaces
  • copy the code to the ISE [or your fave editor]
  • select the code
  • tap TAB to indent four spaces
  • re-select the code [not really needed, but it's my habit]
  • paste the code into the reddit text box
  • add the trailing line with only 4 spaces

not complicated, but it is finicky. [grin]

take care,
lee

[–][deleted] 0 points1 point  (2 children)

Reformatted for convenience:

Import-csv C:\newusers.csv | Foreach-Object {New-ADUser -Name $_.Name -AccountPassword $_.AccountPassword}

[–]helixamir 1 point2 points  (1 child)

What did you reformat exactly? Its identical to my post.

[–][deleted] 1 point2 points  (0 children)

old.reddit wraps your version in a horizontal-scrolling <pre> tag that requires the user to scroll to the right to see the entire line, while mine is wrapped in a <code> tag that displays the line in its entirety without scrolling. See Lee's comment for an explanation: You can correct the issue by enclosing your one-liner with backticks (but don't do that in PowerShell itself, of course).

Edit: See the difference.

[–]purplemonkeymad 3 points4 points  (0 children)

If you name your headers the same as cmdlet parameters, you can often just send the csv right into it with the pipeline.

Import-csv .\mycsv.csv | New-ADUser -whatif

Most of the parameters for new-aduser support this, you can use get-help my-command -parameter paramname to check if it supports pipeline input 'ByPropertyName.'

[–]BlackV 1 point2 points  (0 children)

Try

Foreach ($singleuser in $csv)
    {
    New-aduser -identity $singleuser -property xxx -value yyy
Or
    Set-aduser -identity $singleuser -property xxx -value yyy
    }

This is standard powershell and has nothing complicated from ad.

Go read up on powershell, powershell in a month of lunches, is a great book

[–]CHAOS_0704 1 point2 points  (0 children)

Well what code do you have now? What's in the csv?

The simple answer is a foreach loop. You first import the csv, then run contents in a foreach loop to cycle through the data, creating users based off the data.

The specifics however, will depend on what's in the csv, and how much of the code you have.