Create AD group using powershell

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?

April 26th, 2015 11:19pm

Hi,

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

Free Windows Admin Tool Kit Click here and download it now
April 26th, 2015 11:25pm

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

April 26th, 2015 11:37pm

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.

Free Windows Admin Tool Kit Click here and download it now
April 26th, 2015 11:52pm

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

May 6th, 2015 4:32am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics