This is a two-part question.
One of our user admin duties is to remove a user's group memberships when they leave the firm. I found a script which worked the first few times I tested it but then stopped working for some reason (no error, it just didn't remove the groups). I don't know Powershell well enough to figure out why. I'm hoping that if I can better understand what each line of code does I can figure out where it's going wrong. Below is the code which I have been running as a script file. I'm using Powershell v2, I do remember to import the AD module and set the execution policy to remote signed, and I do have rights to perform this action:
Function RemoveMemberships
{
$user = Get-ADUser -Identity MKG -properties memberof
$userGroups = $user.memberof
$userGroups | %{get-adgroup $_ | Remove-ADGroupMember -confirm:$false -member MKG}
}
In trying to troubleshoot this I changed the confirm to true and it seems the problem is with getting the group memberships - it never prompted me to confirm removal - but I don't know what's wrong or how to fix it. And frankly, experimenting with this particular script is something I'm a bit hesitant to do!
Secondly, is there a way to better control the results of this cmdlet: Get-ADUser - Identity TTESTER -properties memberof | out-file h:\mygroups.txt?
What I'm trying to do here is get a simple list of a user's group memberships and save it to a text file. The results I'm getting include more info than I need. Not a huge deal but if there's a way to truly limit the results just to group memberships, that would be great. Here's what I get:
DistinguishedName : CN=Testera\, Terry T.,OU=Milwaukee,OU=QBUsers,DC=na,DC=qb,DC=llp
Enabled : True
GivenName : Terry
MemberOf : {CN=ALL ASG 1998 FIDUCIARY INCOME TAX,OU=App Security Groups,OU=Firm Wide,DC=na,DC=qb,DC=llp, CN=AL
L ASG 1998 FEDERAL GIFT TAX,OU=App Security Groups,OU=Firm Wide,DC=na,DC=qb,DC=llp}
Name : Testera, Terry T.
ObjectClass : user
ObjectGUID : a870af2b-62cf-46e6-942e-7a44a8554cd3
SamAccountName : TTESTER
SID : S-1-5-21-57989841-1935655697-725345543-63788
Surname : Testera
UserPrincipalName : TTESTER@na.qb.llp