Friday, 9 June 2023

PowerShell - How to create multiple Group along with members using PowerShell

 PowerShell - How to create multiple Group along with members using PowerShell



File Format.

Ensure that your CSV file is formatted like this example:



# Set the OU path

$ouPath = "OU=YourOU,DC=YourDomain,DC=com"


# Specify the path to the file containing group details

$groupDetailsFile = "C:\Temp\GroupDetails.csv"

 

# Read group details from the file

$groupDetails = Import-Csv -Path $groupDetailsFile

 

# Loop through each group detail and create the group

foreach ($groupDetail in $groupDetails) {

    $groupName = $groupDetail.Name

    $memberNames = $groupDetail.Members -split ","

 

    # Create the domain local group within the specified OU

    $group = New-ADGroup -Name $groupName -GroupScope DomainLocal -SamAccountName $groupName -GroupCategory Security -Path $ouPath

 

    # Add members to the group

    foreach ($memberName in $memberNames) {

        Add-ADGroupMember -Identity $groupName -Members $memberName

    }

     # Verify group creation and members

    $group | Get-ADGroup

}

No comments:

Post a Comment