locked
Creating Security Groups via CSV RRS feed

  • Question

  • I have a csv file with the group names I'd like to create, however I want to add a few letters before each name of each group.

    For example:

    GroupA

    GroupB

    GroupC

    How do I create the groups with '123' infront of each name? 

    Import-CSV pathtoCSV
      ForEach-Object{
      $Group = $_.Groups
      New-ADGroup -Name 123$_.Groups -GroupScope Global
    }
      

    I know this isn't correct but I hope that's somewhat clear of what I'm trying to do.

    Thanks!

    Is it also possible to add/nest the new group into the original group in the same script?


    Tuesday, June 13, 2017 12:05 PM

Answers

  • Try it like this, I added the -WhatIf switch so you can see if it works without actually creating anything

    Import-CSV pathtoCSV | ForEach {
      New-ADGroup -Name "123$($_.Groups)" -GroupScope Global -WhatIf
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''



    • Edited by clayman2 Tuesday, June 13, 2017 12:53 PM typo
    • Marked as answer by FishNowWorkNever Tuesday, June 13, 2017 12:59 PM
    Tuesday, June 13, 2017 12:10 PM
  • You already have a variable called $Groups which is the same as $_.Groups, so just use that:

    Import-Csv PathToCSV |
        ForEach-Object{
        $Groups = $_.Groups
        New-ADGroup -Name "123$Groups" -GroupScope Global
        }

    The question that I have though is ... are you sure that $_.Groups contains a single string? I mean I have no idea what your CSV looks like, but bearing in mind it's called 'Groups' I wouldn't expect a single group name to exist there, but this is something you have to know, not me ...

    Tuesday, June 13, 2017 12:52 PM

All replies

  • Try it like this, I added the -WhatIf switch so you can see if it works without actually creating anything

    Import-CSV pathtoCSV | ForEach {
      New-ADGroup -Name "123$($_.Groups)" -GroupScope Global -WhatIf
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''



    • Edited by clayman2 Tuesday, June 13, 2017 12:53 PM typo
    • Marked as answer by FishNowWorkNever Tuesday, June 13, 2017 12:59 PM
    Tuesday, June 13, 2017 12:10 PM
  •  
    Import-Csv PathToCSV |
        ForEach-Object{
        $Groups = $_.Groups
        New-ADGroup -Name "$(123$_.Groups)" -GroupScope Global
        }

    123$_.Groups : The term '123$_.Groups' is not recognized as the name of a cmdlet, function, script file, or operable
    program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:3 char:29

    Tuesday, June 13, 2017 12:44 PM
  • You already have a variable called $Groups which is the same as $_.Groups, so just use that:

    Import-Csv PathToCSV |
        ForEach-Object{
        $Groups = $_.Groups
        New-ADGroup -Name "123$Groups" -GroupScope Global
        }

    The question that I have though is ... are you sure that $_.Groups contains a single string? I mean I have no idea what your CSV looks like, but bearing in mind it's called 'Groups' I wouldn't expect a single group name to exist there, but this is something you have to know, not me ...

    Tuesday, June 13, 2017 12:52 PM
  • I modified above script

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Tuesday, June 13, 2017 12:53 PM
  • Perfect, thanks guys!

    Tuesday, June 13, 2017 12:59 PM