PowerShell - remove single user from all AD group membership w/Log


Apologies for this basic of all basic questions.  I can't seem to get any portion of a script I'm working on to do what I need.

We've the mailboxes of users who have disabled accounts, over to a designated Exchange server.  What I'd like to pull off, can probably be done in just a one-liner, but I'm stuck.

I need to run a script against all users who reside within the server we have designated, then strip those accounts of any and all group membership.  - The problem I've been having is the logging portion, where we need to keep a log of the groups removed...  I can't get anything to log group removal no matter what I try.

$strServerName = '<ServerName>'

$arrModules = @("ActiveDirectory")
ForEach ($strModule In $arrModules) {
      Write-Host "Importing module for $strModule..."
      Try {Import-Module -Name $strModule}
      Catch {
            fncProcessError -strException $($_.Exception.Message) -blnForceExit $True
      Write-Host $vbTab "Import successful."

$AdminSessionADSettings.ViewEntireForest = $True

$arrMBs = Get-Mailbox -Server $strServerName | Select PrimarySMTPAddress
ForEach ($objMB in $arrMBs) {$objUser = Get-ADUser -LDAPFilter "(mail=$objMB.PrimarySmtpAddress)"}

.............   I've gone through a couple iterations of the script...  but haven't gotten to the removal of the groups yet, because I can't get the thing to log.

Any assistance you guys have on this would be very appreciated.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This will remove the groups for all accounts in Users.txt and save a text file for each account that
was modified with all the groups removed.

Foreach ($user in Get-Content "C:\PS\Users.txt") {
    $report = "C:\PS\groups_$((Get-ADUser $user -properties SamAccountName).SamAccountName)_$(Get-date -f dd-MM-yyyy).txt"
    "=============== UserName $user ===============" >> $report
    $Groups = (Get-ADUser $user -Properties memberof).memberof
    $Groups | Get-ADGroup | Select -ExpandProperty Name >> $report
    $Groups | Get-ADGroup | Remove-ADGroupMember -member $user -Confirm:$False

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
usslindstromAuthor Commented:
Very quick and easy solution.  Much appreciated.

I absolutely overlooked just dumping the output.  Thanks for the suggestion.

I need to massage the script so it can handle cross-domain groups - but the logging is spot on.  :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.