locked
Bulk creation of AD groups using existing group names. RRS feed

  • Question

  • Hi All,

    I'm trying to bulk create some new security groups by utilising the names of some existing ones in our AD.

    Essentially, all I'm looking to do is read in the name of the existing group and then append "-Shared" to the end of it when creating the new one.

    I'm thinking this will do the trick, however being a little cautious I wanted to throw this out there for review before I hit the button as -WhatIf doesn't work with script blocks.

    Get-ADGroup -Filter {name -like "<ExistingGroup>*"} | Select-Object name | ForEach-Object {New-ADGroup -Name "$_.-Shared" -DisplayName "$_.-Shared" -Description "Shared folder" -Path "<DistinguishedPathName>" -GroupCategory Security -GroupScope DomainLocal}

    Any expert advice would be greatly appreciated!

    Cheers.

    • Moved by Bill_Stewart Monday, April 30, 2018 9:08 PM This is not "scripts on demand"
    Friday, February 16, 2018 1:11 AM

Answers

  • I would format the code better so that you can clearly see what's going on. The below code will get all the groups that have a category of "Seucrity" and put "-shared" onto the end of the name. Note that I am only writing to the console what the output would be, you would replace this with the "New-ADGroup" commandlet.

    $ExistingGroups = Get-ADGroup -Filter "(groupcategory -eq 'Security)"
    
    foreach ($I in $ExistingGroups){
        $PreGroupName = $i.name
        
        Write-Host "$PreGroupName-Shared"
    }

    Hope this helps :)

    • Marked as answer by Mark_DW Wednesday, March 27, 2019 1:28 AM
    Friday, February 16, 2018 10:05 AM