Welcome Forums General PowerShell Q&A Creating AD Group – How to pass this through?

Viewing 1 reply thread
  • Author
    Posts
    • #167974
      Participant
      Topics: 1
      Replies: 0
      Points: -8
      Rank: Member

      Hi everyone,

      So I’m a little bit new to PowerShell and I’m trying to create a loop to create Distribution Groups within my AD while first creating the set inputs.

      Here is my code so far:

      Import-Module ActiveDirectory
      Set-Location AD:

      $prefix = “Prefix-”

      $name = “Test-Name-1, Test-Name-2, Test-Name-3, Test-Name-4”

      #Append prefix to name

      $distGroupName = $name.Split(‘,’) | ForEach-Object {

      “$prefix$_”

      }

      Foreach ($name in $distGroupName) {New-ADGroup -Name $_.name -DisplayName $_.name) -GroupCategory Distribution -GroupScope Global -Path “OU=Distribution,OU=EXAMPLE,DC=GLOBAL,DC=COM”}

       

      When I’m trying this I’m getting: Cannot validate argument on parameter ‘Name’. the argument is null or empty. Provide an argument that is not null or empty, and then try the command again. 

      I’m trying to pass off the joined $prefix and $name in the array $distGroupName to New-ADGroup. How would I go about doing this?

       

      Thank you,

      David Thao

    • #167998
      Participant
      Topics: 7
      Replies: 13
      Points: 65
      Rank: Member

      Hi David!

       

       

       

      #I suggest checking the results of your variables!
      
      
      
      $distGroupName = $name.Split(',') | ForEach-Object {
      
      "$prefix$_"
      
      }
      
      
      
      write-verbose "$distGroupName" -verbose
      
      
      Foreach ($name in $distGroupName) {
      write-verbose "$name" -verbose
      pause
      New-ADGroup -Name $_.name -DisplayName $_.name) -GroupCategory Distribution -GroupScope Global -Path "OU=Distribution,OU=EXAMPLE,DC=GLOBAL,DC=COM"}
Viewing 1 reply thread
  • The topic ‘Creating AD Group – How to pass this through?’ is closed to new replies.