Public/TenantConfiguration/Baseline/Add-BaselineGroups.ps1

function Add-BaselineGroups {
    param(
        [Parameter(Mandatory)]
        [string]$TenantId
    )
  
    try {
        Connect-CustomerGraph -CustomerTenantId $TenantId
  
        # Get all groups
        $Groups = Get-MgGroup -All

        # Get all baseline groups
        $BaselineGroupsFiles = Get-ChildItem -Path "$PSScriptRoot\BaselineGroups" -Filter *.json
        foreach($BaselineGroupsFile in $BaselineGroupsFiles) {
            $BaselineGroup = Get-Content -Path $BaselineGroupsFile.FullName | ConvertFrom-Json -AsHashtable -Depth 100
            if($Groups.displayName -contains $BaselineGroup.displayName) {
                Write-Host "Group '$($BaselineGroup.displayName)' already exists, not creating.." -ForegroundColor Yellow
            }
            else {
                $Group = Invoke-MgGraphRequest -Method POST -Uri "https://graph.microsoft.com/beta/groups" -Body $BaselineGroup
                Write-Host "Created group '$($BaselineGroup.displayName)'." -ForegroundColor Green
            }
        }
    }
    catch {
        throw "Failed to add baseline groups: $_"
    }
}