locked
Create AD group using powershell RRS feed

  • Question

  • foreach ($item in $list) {
     If ($item.GroupName -eq "") {
            Write-Host "[ERROR]`t Please provide valid group details in spreadsheet."  -ForegroundColor Red
          } Else {
          $item.ID = $ou
        New-ADGroup -Name $item.GroupName –path “OU=$ou,DC=account,DC=company,DC=com”  -GroupCategory Security -GroupScope DomainLocal

    }

    I am getting a syntax error because of OU=$ou. Any idea how to fix this?

    Monday, April 27, 2015 3:15 AM

Answers

  • It looks like you might have some smartquotes in your script. Manually retype all of your quotes and try again.

    If that doesn't help, post the first few lines of your CSV.


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Proposed as answer by AnnaWY Monday, May 4, 2015 8:44 AM
    • Marked as answer by AnnaWY Sunday, May 10, 2015 10:05 AM
    Monday, April 27, 2015 3:47 AM
  • Hi VicVmware,

    I'm confused about this line "$item.ID = $ou", I assume you want to read "ID" property from csv file, and assign this value to OU, in this case, please reverse this line:

    $ou = $item.ID
     New-ADGroup -Name $item.GroupName –path “OU=$ou,DC=company,DC=com”  -GroupCategory Security -GroupScope DomainLocal

    If there is anything else regarding this issue, please feel free to post back.

    Best Regards,

    Anna Wang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    • Proposed as answer by AnnaWY Wednesday, May 6, 2015 8:26 AM
    • Marked as answer by AnnaWY Sunday, May 10, 2015 10:05 AM
    Wednesday, May 6, 2015 8:24 AM

All replies

  • Hi,

    What is the actual error message? Also, post the first few lines of your input file.


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Monday, April 27, 2015 3:21 AM
  • Try {
      Import-Module ActiveDirectory -ErrorAction Stop
    }
    Catch {
      Write-Host "[ERROR]`t ActiveDirectory Module couldn't be loaded. Script will stop!" -ForegroundColor Red
      Exit 1
    }

    $path     = Split-Path -parent $MyInvocation.MyCommand.Definition
    $newpath  = $path + "\creategroups.csv"
    $list = Import-Csv $newpath


    Function Start-Commands {
      Create-Groups
    }


    Function Create-Groups {

    foreach ($item in $list) {
     If ($item.GroupName -eq "") {
            Write-Host "[ERROR]`t Please provide valid group details."  -ForegroundColor Red
          } Else {
          $item.ID = $ou
        New-ADGroup -Name $item.GroupName –path “OU=$ou,DC=company,DC=com”  -GroupCategory Security -GroupScope DomainLocal
        Write-Host "[INFO]`t Created new group : $($item.GroupName)" -ForegroundColor Green

            }    }
    }

    Write-Host "STARTED SCRIPT`r`n"  -ForegroundColor Cyan
    Start-Commands
    Write-Host "STOPPED SCRIPT" -ForegroundColor Cyan

    New-ADGroup : The server is unwilling to process the request
    At C:\Users\t-user-aws\Desktop\test\CreateGroups.ps1:29 char:5
    +     New-ADGroup -Name $item.GroupName –path 'OU=$item.ID,DC=DC=company, ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (CN=TestGroupB,O...as,DC=com:String) [New-ADGroup], ADException
        + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.NewADGroup

    Monday, April 27, 2015 3:33 AM
  • It looks like you might have some smartquotes in your script. Manually retype all of your quotes and try again.

    If that doesn't help, post the first few lines of your CSV.


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Proposed as answer by AnnaWY Monday, May 4, 2015 8:44 AM
    • Marked as answer by AnnaWY Sunday, May 10, 2015 10:05 AM
    Monday, April 27, 2015 3:47 AM
  • Hi VicVmware,

    I'm confused about this line "$item.ID = $ou", I assume you want to read "ID" property from csv file, and assign this value to OU, in this case, please reverse this line:

    $ou = $item.ID
     New-ADGroup -Name $item.GroupName –path “OU=$ou,DC=company,DC=com”  -GroupCategory Security -GroupScope DomainLocal

    If there is anything else regarding this issue, please feel free to post back.

    Best Regards,

    Anna Wang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    • Proposed as answer by AnnaWY Wednesday, May 6, 2015 8:26 AM
    • Marked as answer by AnnaWY Sunday, May 10, 2015 10:05 AM
    Wednesday, May 6, 2015 8:24 AM