Forum Discussion
ADumith
Apr 19, 2023Iron Contributor
Removing user from all groups in an AD
Hello everyone, I'm trying to use this script to accept input from the user based on what user they want removed from all groups, but I need the script make sure that the user account is not ena...
ADumith
Apr 20, 2023Iron Contributor
Hello Andres-Bohren
I was testing the script, but I'm not sure if there is something wrong on my end or this is expected.
Any how, when I enter a wrong user name, I'm getting this message:
Get-ADUser : Cannot find an object with identity: 'xyzqwe3' under: 'DC=mydom,DC=xft'.
At line:3 char:11
+ $ADUser = Get-ADUser -Identity $Username -Properties MemberOf
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (xyzqwe3:ADUser) [Get-ADUser], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Thank you in advance,
Andres-Bohren
Apr 20, 2023Iron Contributor
Change this line
$ADUser = Get-ADUser -Identity $Username -Properties MemberOf -ErrorAction SilentlyContinue
$ADUser = Get-ADUser -Identity $Username -Properties MemberOf -ErrorAction SilentlyContinue