Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Getting the power shell error while remove user from the group

crazysanty0
Giga Contributor

I am trying to remove member from AD group but i am getting below error.

power shell script :
Import-Module ActiveDirectory

# === Configuración ===
$GroupName = "I_abc"

try {
Write-Host "Getting members of group '$GroupName'..." -ForegroundColor Cyan
$members = Get-ADGroupMember -Identity $GroupName -ErrorAction Stop

if ($members.Count -eq 0) {
Write-Host "Group '$GroupName' has no members to remove." -ForegroundColor Yellow
}
else {
Write-Host "Found $($members.Count) member(s) — removing them..." -ForegroundColor Cyan
Remove-ADGroupMember -Identity $GroupName -Members $members -Confirm:$false
Write-Host " Removed all $($members.Count) members from '$GroupName'." -ForegroundColor Green
}
}
catch {
Write-Host " Error: $($_.Exception.Message)" -ForegroundColor Red
}

Error:
contextId=59b20dad8601be90f86e1fec725c114c DEBUG: No valid credential found, running script without credential

1 REPLY 1

palanikumar
Giga Sage

Credentials are missing. Use the below sample script

 

Import-Module ActiveDirectory

# === Configuración ===
$GroupName = "I_abc"
$username = "domain\username"
$password = ConvertTo-SecureString "YourPassword" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($username, $password)

try {
  Write-Host "Getting members of group '$GroupName'..." -ForegroundColor Cyan
  $members = Get-ADGroupMember -Identity $GroupName -Credential $cred -ErrorAction Stop

  if ($members.Count -eq 0) {
    Write-Host "Group '$GroupName' has no members to remove." -ForegroundColor Yellow
  }
  else {
    Write-Host "Found $($members.Count) member(s) — removing them..." -ForegroundColor Cyan
    Remove-ADGroupMember -Identity $GroupName -Members $members -Credential $cred -Confirm:$false
    Write-Host ":white_heavy_check_mark: Removed all $($members.Count) members from '$GroupName'." -ForegroundColor Green
  }
}
catch {
  Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
}
Thank you,
Palani