Link to home
Start Free TrialLog in
Avatar of Domenic DiPasquale
Domenic DiPasqualeFlag for United States of America

asked on

Remove groups from user objects in a specific OU folder in active directory.

I need to create a power shell script that will remove all the groups from a user object, except the Domain User group. I found this sample and made the changes needed to point to the OU folder that will contain the users that need the groups removed from them.

Import-Module activedirectory
$ou = Get‐ADUser ‐SearchBase "OU=Disabled Users,DC=LABDOMAIN,DC=COM" ‐Filter *
foreach ($user in $ou) {
$UserDN = $user.DistinguishedName
Get‐ADGroup ‐LDAPFilter "(member=$UserDN)" | foreach‐object {
if ($_.name -ne "Domain Users") {remove‐adgroupmember ‐identity $_.name ‐member $UserDN ‐Confirm:$False} }
}

When I run the script, I receive the errors below.
Get‐ADUser : The term 'Get‐ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At C:\Users\Administrator\Desktop\User Object Cleanup.ps1:2 char:7
+ $ou = Get‐ADUser ‐SearchBase "OU=Disabled Users,DC=LABDOMAIN,DC=COM" ‐Filter *
+       ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get‐ADUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of Will Szymkowski
Will Szymkowski
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Domenic DiPasquale

ASKER

I found my issue. "-member" was missing an "s" at the end. I have "Import-Module activedirectory" in front of the code, which should enable the use of Get-ADUser. I decided to re-write the code using the PowerShell ISE to make sure the syntax was entered properly.
Get‐ADUser : The term 'Get‐ADUser' is not recognized as the name of a cmdlet
You get this error message specifically when the module is not imported into the powershell session.

Will.