Hi,
I made some modifications to your script. Hope this works for you.
$Groups = Get-Content -Path c:\ADGroupsMemberExport.txt
$TargetOU = "OU=_UnusedADGroups,DC=test,DC=local"
$Groups | Get-ADGroup -Properties Member | ForEach-Object{
##Move all Distribution or Security Groups that are empty to new OU.
if($_.member.count -eq 0) {
Move-ADObject -Identity $_ -TargetPath $TargetOU
}
##Remove members from non-empty Distribution groups and move the group to new OU.
elseif ($_.GroupCategory -eq 'Distribution')
{
Remove-ADGroupMember -Identity $_ -Members (Get-ADGroupMember -Identity $_) -Confirm:$False -PassThru | Move-ADObject -TargetPath $TargetOU
}
##Convert non-empty Distribution groups to Security group and move the group to new OU.
elseif ($_.GroupCategory -eq 'Security')
{
Set-ADGroup -Identity $_ -GroupCategory Distribution -PassThru | Move-ADObject -TargetPath $TargetOU
}
}
Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.