Home > Software engineering >  Error when trying to bulk create new-adgroup from csv file
Error when trying to bulk create new-adgroup from csv file

Time:08-19

I' am trying to create a bunch of AD groups using New-ADGroup. When I run the script I get the following error:

New-ADGroup : Cannot validate argument on parameter 'GroupCategory'. The argument is null. Provide a valid value for the argume nt, and then try running the command again.

The script I' am running is:

Import-Module ActiveDirectory

#Import CSV
$groups = Import-Csv '\\server\path\to\file\myfile.csv'


# Loop through the CSV
    foreach ($group in $groups) {

    $groupProps = @{

      Name          = $group.name
      Path          = $group.path
      GroupCategory = $group.category
      GroupScope    = $group.scope
      Description   = $group.description

      }#end groupProps

    New-ADGroup @groupProps
    
} #end foreach loop

My csv file and columns: my file

I tried to swap the order of parameters, originally is was name, path, scope, category, description however, it returns the same error.

I read the documentation for New-ADGroup and the code seems right, so I' am kind of stuck right now.

I used this to create the script and .csv file: https://activedirectorypro.com/create-active-directory-security-groups-with-powershell/

CodePudding user response:

`` Import-Module ActiveDirectory

#Import CSV $groups = Import-Csv '\server\path\to\file\myfile.csv'

Loop through the CSV

foreach ($group in $groups) {

$groupProps = @{

  Name          = $($group.name)
  Path          = $($group.path)
  GroupCategory = $($group.category)
  GroupScope    = $$group.scope)
  Description   = $($group.description)

  }#end groupProps

New-ADGroup @groupProps

} #end foreach loop``

CodePudding user response:

Found the solution. As Mathias R. Jessen said it was the delimiter.

Fixed it by adding -Delimiter ";" at the end of $groups = Import-Csv '\\server\path\to\file\myfile.csv'

So it ended up like this:

$groups = Import-Csv '\\server\path\to\file\myfile.csv' -Delimiter ";"
  • Related