Skip to content

Instantly share code, notes, and snippets.

@AspenForester
Created May 16, 2014 16:36
Show Gist options
  • Save AspenForester/aa0e25f17772775024dc to your computer and use it in GitHub Desktop.
Save AspenForester/aa0e25f17772775024dc to your computer and use it in GitHub Desktop.
A couple of quick functions for AD group maintenance
# requires version 3.0
<# Clear-ADGroupMember
.Synopsis
Removes all users from an AD group
.DESCRIPTION
Removes all users from an AD group provided to the script.
.EXAMPLE
“my-group” | Clear-ADGroupMember
#>
function Clear-ADGroupMember {
[CmdletBinding()]
#[OutputType([Microsoft.ActiveDirectory.Management.ADGroup])]
Param(
# Identity of Group to depopulate
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
$Identity
)
Begin
{
}
Process
{
$ExistingGroup = Get-ADGroupMember $Identity
Remove-ADGroupMember -Identity $Identity -Members $ExistingGroup
}
End
{
}
} # End Function Clear-ADGroupMember
Function Get-UnusedGroup {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True)]
[string]
$searchBase
)
Get-ADGroup -Filter * -Properties members, isCriticalSystemObject -SearchBase $searchBase |
Where-Object { ($_.members.count -eq 0 -AND !($_.IsCriticalSystemObject) -AND $_.DistinguishedName -notMatch 'Exchange Security' -AND $_.DistinguishedName -notMatch 'Dns') }
$searchBase = $null
} #end function Get-UnusedGroup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment