Home

Powershell to remove empty security groups

%3CLINGO-SUB%20id%3D%22lingo-sub-61765%22%20slang%3D%22en-US%22%3EPowershell%20to%20remove%20empty%20security%20groups%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-61765%22%20slang%3D%22en-US%22%3E%3CP%3EJust%20getting%20into%20Azure%20and%20imported%20a%20bunch%20of%20empty%20Security%20Groups%20into%20Azure%20AD.%20%26nbsp%3BTrying%20to%20sort%20out%20a%20way%20to%20delete%20Groups%20if%20they%20have%200%20members.%20%26nbsp%3BAnyone%20got%20a%20quick%20way%20to%20do%20that%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you.%3C%2FP%3E%3CP%3EChris%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-61765%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20AD%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-61809%22%20slang%3D%22en-US%22%3ERE%3A%20Powershell%20to%20remove%20empty%20security%20groups%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-61809%22%20slang%3D%22en-US%22%3E%3CA%20href%3D%22http%3A%2F%2Fwww.garethjones294.com%2Fbulk-removing-azure-active-directory-users-and-groups-using-powershell%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fwww.garethjones294.com%2Fbulk-removing-azure-active-directory-users-and-groups-using-powershell%2F%3C%2FA%3E%20Used%20your%20suggestion%20with%20this%20blog%20post%20and%20found%20what%20I%20needed.%20Thank%20you%20again.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-61785%22%20slang%3D%22en-US%22%3ERE%3A%20Powershell%20to%20remove%20empty%20security%20groups%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-61785%22%20slang%3D%22en-US%22%3EThank%20you%2C%20that%20does%20get%20me%20a%20list%20of%20empty%20groups%2C%20I%20can%20pipe%20that%20to%20a%20file%20and%20use%20that%20to%20remove%20them%20I%20think.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-61780%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20to%20remove%20empty%20security%20groups%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-61780%22%20slang%3D%22en-US%22%3E%3CP%3ETry%20something%20like%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGet-MsolGroup%20-GroupType%20Security%20%7C%20%3F%20%7B(Get-MsolGroupMember%20-GroupObjectId%20%24_.ObjectId).count%20-eq%200%20%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E
null null
New Contributor

Just getting into Azure and imported a bunch of empty Security Groups into Azure AD.  Trying to sort out a way to delete Groups if they have 0 members.  Anyone got a quick way to do that?

 

Thank you.

Chris

3 Replies

Try something like this:

 

Get-MsolGroup -GroupType Security | ? {(Get-MsolGroupMember -GroupObjectId $_.ObjectId).count -eq 0 }

Thank you, that does get me a list of empty groups, I can pipe that to a file and use that to remove them I think.
http://www.garethjones294.com/bulk-removing-azure-active-directory-users-and-groups-using-powershell... Used your suggestion with this blog post and found what I needed. Thank you again.
Related Conversations
Extentions Synchronization
Deleted in Discussions on
3 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
36 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies