How to create AD Groups via string+variable?

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Roberto Dalmacion Roberto Dalmacion 8 months, 3 weeks ago.

  • Author
    Posts
  • #36615
    Profile photo of Roberto Dalmacion
    Roberto Dalmacion
    Participant

    Hello,

    I am building delegation groups for a new OU structure design and the naming convention is based on the group role and site location (ie ADL- admin local, newyork – site. ) I would like to know if it is possible to create new groups based on the different sites from a text file/csv with the group name added to a string to conform with the naming convention?

    This is what I had in mind:

    $strGrpSite = Get-Content .\Sites.txt

    $strGrpSite|Foreach{
    New-ADGroup "ADL-"+$_"_Create" -GroupScope DomainLocal -GroupCategory Security -path "OU=OU=TestOU,DC=testdomain,DC=com"
    }

    The intended output would be:
    New group created: ADL-newyork_Create

    What I do not know is the correct syntax to join the strings to form the groupname.

  • #36622
    Profile photo of Roberto Dalmacion
    Roberto Dalmacion
    Participant

    Nevermind, I figured it out..

    Sites.txt contains:
    newyork
    miami
    london

    $strGrpSite = Get-Content .\Sites.txt

    $strGrpSite|Foreach{
    $strGrpName = -join("ADL-","$_","_Create"
    New-ADGroup $strGrpName -GroupScope DomainLocal -GroupCategory Security -path "OU=OU=TestOU,DC=testdomain,DC=com"
    }

    Result Output:

    ADL-newyork_Create
    ADL-miami_Create
    ADL-london_Create

  • #36624
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    Try something like this:
    New-ADGroup -name "ADL-$strgroup_Create"

  • #36628
    Profile photo of Roberto Dalmacion
    Roberto Dalmacion
    Participant

    That won't work since Powershell will consider "$strgroup_Create" as the whole variable name instead of separate strings. I have already tested this out and it only created the "ADL-" part.

    This definitely worked though:
    $strGrpName = -join("ADL-","$_","_Create")

    Thanks anyway for the response.

    Cheers!

You must be logged in to reply to this topic.