Skip to content

Instantly share code, notes, and snippets.

@Pome-ro
Created April 19, 2021 15:19
Show Gist options
  • Save Pome-ro/b07437d7434d64f976e302d204700824 to your computer and use it in GitHub Desktop.
Save Pome-ro/b07437d7434d64f976e302d204700824 to your computer and use it in GitHub Desktop.
Remove User from Group shell menu
function Remove-ADUserFromGroup {
[CmdletBinding()]
param (
[Parameter(mandatory)]
[string]
$identity,
[Parameter(mandatory)]
[string]
$credential
)
$ADUser = Get-ADUser $identity -Properties memberof
$Selections = (($ADUser).memberof | Get-ADGroup) | Select Name,GroupCategory,GroupScope| Sort-Object Name
Write-Host "Select Group to remove."
Write-Host "----------------------"
ForEach($Thing in $Selections) {
$index = [array]::indexof($selections,$Thing)
Write-Host "$index) $($Thing.Name)"
}
Write-Host "Q) QUIT"
$Choice = Read-Host "Please make a selection"
if ($choice -eq 'Q') {
return
} elseif ($choice -ne $null) {
Write-Host "You are about to remove $($aduser.name) from the group $($selections[$choice].name)" -ForegroundColor Red
Write-Host "Do you wish to proceed?" -ForegroundColor Red
Write-Host "Y) Yes"
Write-Host "N) No"
$Confirm = Read-Host "Please make a selection"
switch ($confirm) {
"Y" { Remove-ADGroupMember -Identity $($Selections[$choice].Name) -Members $ADUser -Credential $cred }
"N" {Return}
Default {Return}
}
} else {
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment