Apologies if this has already been answered else where, I'm trying to run the code below but it's giving me 'Cannot validate argument on parameter 'identity'. The identity property on the argument is null or empty'.
I know this is going to be something simple but can someone provide some pointers, many thanks, the first member of the list works all ok, it's just from the second member the above error appears, when I check $account I get a correct value for an expected SAM account name.
$AccountList = (Get-ADUser -Filter "SAMAccountName -like '*$SAMAccountName'" | select SAMAccountName)
ForEach($Account in $AccountList){
Get Group List
Try {
$GroupList = (Get-ADUser $Account -properties memberOf).memberOf
Set-ADUser -Identity $Account -clear extensionattribute2
ForEach($group in $groupList) {
Remove-ADGroupMember -Identity $group -Members $Account -Confirm:$False
}
} catch {
$completionDetails = "There was an error removing the groups: $($error.Exception)"
$completionDetails += "`nPlease investigate manually.`n"
}}