You might be able to consolidate that code even further, eliminating the call to Get-ADGroup. I’m not at home, so I can’t test this code yet, but you can try it:
$Groups = Import-Csv C:\Scripts\CSVFILE.csv -UseCulture foreach ($csvEntry in $groups) { try { $group = New-ADGroup -PassThru -Name $csvEntry.Name -Description $csvEntry.Description -GroupCategory "Security" -GroupScope "Universal" -DisplayName $csvEntry.displayNamePrintable -Path $csvEntry.Path -SamAccountName $csvEntry.SamAccountName -Verbose -ErrorAction Stop $GID = [int]$group.SID.Value.Substring([int]$group.SID.Value.Lastindexof("-")+1)+1000 $group | Set-ADGroup -Replace @{gidNumber=$GID} -Verbose -ErrorAction Stop } catch { # handle error however you like } }
Edit: Forgot the -PassThru parameter to New-ADGroup