none
Powershell Script : Add mulitple users to different OU in same domain RRS feed

  • Question

  • How to add multiple users, while also adding them to different OUs (in one script) in the same domain?

    Thanks in advance!

    Monday, June 30, 2014 4:30 PM

Answers

All replies

  • What have you tried so far and with what results?


    PS C:\> help Move-ADObject
    


    -- Bill Stewart [Bill_Stewart]

    Monday, June 30, 2014 4:36 PM
    Moderator
  • My csv file has the columns FirstName,LastName,Unit.  The script I'm using is:

    Import-CSV UsersImport.csv | ForEach-Object {New-ADUser -SamAccountName ($_.FirstName+"."+$_.Lastname) -Name

    ($_.FirstName+" "+$_.LastName) -path ($_.unit) -AccountPassword (ConvertTo-SecureString -AsPlainText "P@$$w0rd123" -Force) -Enabled $true -PasswordNeverExpires $True}

    Monday, June 30, 2014 5:11 PM
  • So what's the question then?


    -- Bill Stewart [Bill_Stewart]

    Monday, June 30, 2014 5:13 PM
    Moderator
  • Apologies, Bill.  I tried the script, and it's doesn't work.  I'm new to Powershell, so I'm not sure what my script is missing.  Or, if there could be another way to import my csv file with the column headers I have, so the user will be created, going to the OU it belongs to.  Hope that makes sense.

    What I have for user information is:

    Jackson,Stein,"ou=this is test,dc=test,dc=com"

    Thx,

    magnolia88

    Monday, June 30, 2014 5:16 PM
  • Remember that we have no information about your problem other than what you post here. (We can't see your screen!)

    Please read the following for information about the purpose of this forum and also how to ask a good question:


    -- Bill Stewart [Bill_Stewart]

    Monday, June 30, 2014 5:18 PM
    Moderator
  • Here are some hints on how to do this.

    Import-CSV UsersImport.csv |
         ForEach-Object{
              $userprops=@{
                   SamAccountName= "$($_.FirstName).$($_.Lastname)"
                   Name="$($_.FirstName).$($_.Lastname)"
                   Path=$_.unit
                   AccountPassword=ConvertTo-SecureString -AsPlainText "P@$$w0rd123" -Force)
                   Enabled=$true
                   PasswordNeverExpires=$true
              }
    
              $newuser=New-ADUser  @userProps
              Add-AdGroupMember 'Mygroup1' -member $newuser
              Add-AdGroupMember 'Mygroup2' -member $newuser
              Add-AdGroupMember 'Mygroup3' -member $newuser 
    }

    Proper formatting can help you scritp to be readable.

    You need to study each CmdLet.  Try each one on a test case until you understand how they work.  Use the learning resources.  Check the scripts in the repository.

    We cannot write a script for you and you need to spen time learning the fundamentals.


    ¯\_(ツ)_/¯


    • Edited by jrv Monday, June 30, 2014 5:25 PM
    Monday, June 30, 2014 5:21 PM
  • Thanks, Bill.  I'll be more careful in the future.  I don't want script written for me.  I'm just trying to figure out what I'm doing wrong.  Right now, I'm doing this all in a test environment.
    Monday, June 30, 2014 5:24 PM
  • It is good to try to figure out what you're doing wrong.

    There's no way for us to help you do that because we can't see what's happening. We don't know the error message(s) you're getting because you have not told us what they are. (Again: We can't see your screen!)

    Step back and try to break down your problem into single steps. Try only one step at a time. If you have a question about it, specify enough details so that someone else can reproduce it, and also tell the exact error(s) you're getting, if any.


    -- Bill Stewart [Bill_Stewart]

    Monday, June 30, 2014 5:28 PM
    Moderator
  • Thanks, Bill.  I'll be more careful in the future.  I don't want script written for me.  I'm just trying to figure out what I'm doing wrong.  Right now, I'm doing this all in a test environment.

    I think what Bill is trying to make you aware of is that your question is a bit ambiguous.  You can find all  of the answers to crating a new user in the help.

    I think you are really asking about "Path"

    Try using help

    help New-AdUser -par path


    ¯\_(ツ)_/¯

    Monday, June 30, 2014 5:32 PM
  • Adjusting for the possibly different question:

    Import-CSV UsersImport.csv |
         ForEach-Object{
              $userprops=@{
                   SamAccountName= "$($_.FirstName).$($_.Lastname)"
                   Name="$($_.FirstName).$($_.Lastname)"
                   Path=$_.unit
                   AccountPassword=ConvertTo-SecureString -AsPlainText "P@$$w0rd123" -Force)
                   Enabled=$true
                   PasswordNeverExpires=$true
              }
    
              $newuser=New-ADUser  @userProps
    }
    

    This adds users to the path specified in the CVS at $_.Unit


    ¯\_(ツ)_/¯

    Monday, June 30, 2014 5:33 PM
  • Thanks, JV.  I appreciate the assist.  I'm determined to figure it out...
    Monday, June 30, 2014 5:37 PM