Domenic DiPasquale
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\Des ktop\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?
Import-Module activedirectory
$ou = Get‐ADUser ‐SearchBase "OU=Disabled Users,DC=LABDOMAIN,DC=COM"
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\Des
+ $ou = Get‐ADUser ‐SearchBase "OU=Disabled Users,DC=LABDOMAIN,DC=COM"
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get‐ADUser:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Get‐ADUser : The term 'Get‐ADUser' is not recognized as the name of a cmdletYou get this error message specifically when the module is not imported into the powershell session.
Will.
ASKER