Need some help with PS scripts

Welcome Forums General PowerShell Q&A Need some help with PS scripts

This topic contains 4 replies, has 4 voices, and was last updated by

1 year, 1 month ago.

  • Author
  • #98502

    Topics: 2
    Replies: 5
    Points: 28
    Rank: Member


    I'm need some help with creating PS script, what I need is PS that creates bulk OU, Group, User in my AD.

    I have been trying it all morning to get this done, I get close but it fails.

    I want to import all from one CSV file.

  • #98505

    Topics: 13
    Replies: 4872
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    Did you have a question? We're not able to write an entire script for you.

  • #98584

    Topics: 2
    Replies: 933
    Points: 1,611
    Helping Hand
    Rank: Community Hero

    Break what you are doing into steps, one-liners

    • create OU's
    • create groups
    • create users
    • add user to groups
    • etc..

    then put into a function.

    Do this one at time using the Windows Server Active Directory Administrative Center. It will writ eth code for you to tweak later for bulk operations.

  • #98611

    Topics: 2
    Replies: 5
    Points: 28
    Rank: Member

    Thank you D.J. & postanote.

    Here is my existing script

    Import-Module ActiveDirectory
    Import-Csv "C:\Users-Imported\NewUsers100.csv" | ForEach-Object {
    $userPrincinpal = $_."samAccountName" + "@TestDomain.Local"
    New-ADUser -Name $_.Name `
    -Path $_."ParentOU" `
    -SamAccountName $_."samAccountName" `
    -UserPrincipalName $userPrincinpal `
    -AccountPassword (ConvertTo-SecureString "MyPassword123" -AsPlainText -Force) `
    -ChangePasswordAtLogon $true `
    -Enabled $true
    Add-ADGroupMember "Remote Desktop Users" $_."samAccountName";


    Here is my file:
    Bim,bayle,bim byle,Byle Bim,bbim,"OU=Enterprise Administration,DC=pmo,DC=cloud,DC=com",OU=testou
    Kim,Kayle,Kim Kyle,Kyle Kim,kkim,"OU=Enterprise Administration,DC=pmo,DC=cloud,DC=com",OU=testou1
    Jim,Jayle,Jim Jayle,Jim Jayle,jjayle,"OU=Enterprise Administration,DC=pmo,DC=cloud,DC=com",OU=testou2
    Tim,Tayle,Tim Tayle,Tim Tayle,ttayle,"OU=Enterprise Administration,DC=pmo,DC=cloud,DC=com",OU=existingOU


    It fails since I don't have "testou, testou1, testou2" created and I know that I need to use New-ADOrganizational Unit but not sure what is the best way to do it and to omit "existingOU"

  • #98619

    Topics: 2
    Replies: 437
    Points: 875
    Helping Hand
    Rank: Major Contributor

    I would probably recommend splitting these tasks — get a list of the OUs you plan on creating, create them, then create your users. Trying to do it all at once seems more likely to give you a headache when you could be making it a good bit easier on yourself. 🙂

    You could even automate that step (getting the OU list you want) if your file is big enough, but you'll need a way to exclude the existing OUs, maybe...

    Something like...

    $NewOUList = Import-Csv -Path 'C:\Path\To\File.csv' |
        Select-Object -Property ParentOU, NewOU |
        ForEach-Object {
            $_.NewOU, $_.ParentOU -join ','
    # Use $NewOUList to make each OU here

The topic ‘Need some help with PS scripts’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort