troubleshooting Question

Remove group membership from user in AD

Avatar of davidthegnome2003
davidthegnome2003 asked on
Active DirectoryOutlook
6 Comments1 Solution107 ViewsLast Modified:
I am using this PowerShell script to attempt to remove groups from disabled users, but I am getting an error. I need help to make the script work.

import-module activedirectory
$list = Import-Csv c:\user\DN.csv
foreach ($entry in $list)
$UserDN = $entry.DistinguishedName
Get-ADGroup -LDAPFilter "(member=$UserDN)" | foreach-object {
if ($ -ne "Domain Users") {
try {
remove-adgroupmember -identity $ -member $UserDN -Confirm:$False} }
catch [ADexcption] {
write-output "Error Deleting User:" $


Missing statement body in foreach loop.
At C:\Scripts\Exit User.ps1:4 char:1
+  <<<< $UserDN = $entry.DistinguishedName
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingForeachStatement
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros