Hey all,<\/p>\n
I’m trying to use the script to take a list of terminated employees and disabled them/remove them from all groups. It’s not working.<\/p>\n
I’m not sure why. The error I’m getting is cannot validate argument on parameter “Identity.”<\/p>\n
Any help is appreciated.<\/p>\n
$csvFile = \"pathtocsv.xlsx\"\n$disabledUsersOU = \"OU=Disabled Users,DC=internal,DC=contoso,DC=com\"\n\nImport-Csv $csvFile | ForEach-Object {\n\t# Disable the account\n\tDisable-ADAccount -Identity $_.UserName\n\t# Retrieve the user object and MemberOf property\n\t$user = Get-ADUser -Identity $_.UserName -Properties MemberOf\n\t# Move user object to disabled users OU\n\t$user | Move-ADObject -TargetPath $disabledUsersOU\n\t# Remove all group memberships (will leave Domain Users as this is NOT in the MemberOf property returned by Get-ADUser)\n\tforeach ($group in ($user | Select-Object -ExpandProperty MemberOf))\n\t{\n\t\tRemove-ADGroupMember -Identity $group -Members $user\n\t}\n \n}\n<\/code><\/pre>","upvoteCount":3,"answerCount":7,"datePublished":"2017-08-07T13:00:12.000Z","author":{"@type":"Person","name":"jacobdurham","url":"https://community.spiceworks.com/u/jacobdurham"},"acceptedAnswer":{"@type":"Answer","text":"What is the exact error message that you get? The error message usually will list the line in the script where the error occurred. That will tell you which command had the error.<\/p>\n
What does the formatting of your csv file look like? Does the csv file have headers?<\/p>","upvoteCount":1,"datePublished":"2017-08-07T13:04:57.000Z","url":"https://community.spiceworks.com/t/powershell-script-help-ad-modules/598246/3","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},"suggestedAnswer":[{"@type":"Answer","text":"
Hey all,<\/p>\n
I’m trying to use the script to take a list of terminated employees and disabled them/remove them from all groups. It’s not working.<\/p>\n
I’m not sure why. The error I’m getting is cannot validate argument on parameter “Identity.”<\/p>\n
Any help is appreciated.<\/p>\n
$csvFile = \"pathtocsv.xlsx\"\n$disabledUsersOU = \"OU=Disabled Users,DC=internal,DC=contoso,DC=com\"\n\nImport-Csv $csvFile | ForEach-Object {\n\t# Disable the account\n\tDisable-ADAccount -Identity $_.UserName\n\t# Retrieve the user object and MemberOf property\n\t$user = Get-ADUser -Identity $_.UserName -Properties MemberOf\n\t# Move user object to disabled users OU\n\t$user | Move-ADObject -TargetPath $disabledUsersOU\n\t# Remove all group memberships (will leave Domain Users as this is NOT in the MemberOf property returned by Get-ADUser)\n\tforeach ($group in ($user | Select-Object -ExpandProperty MemberOf))\n\t{\n\t\tRemove-ADGroupMember -Identity $group -Members $user\n\t}\n \n}\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2017-08-07T13:00:12.000Z","url":"https://community.spiceworks.com/t/powershell-script-help-ad-modules/598246/1","author":{"@type":"Person","name":"jacobdurham","url":"https://community.spiceworks.com/u/jacobdurham"}},{"@type":"Answer","text":"Forgot to save it as a CSV.<\/p>","upvoteCount":0,"datePublished":"2017-08-07T13:04:29.000Z","url":"https://community.spiceworks.com/t/powershell-script-help-ad-modules/598246/2","author":{"@type":"Person","name":"jacobdurham","url":"https://community.spiceworks.com/u/jacobdurham"}},{"@type":"Answer","text":"
It sounds like $group is invalid.<\/p>","upvoteCount":1,"datePublished":"2017-08-07T13:05:47.000Z","url":"https://community.spiceworks.com/t/powershell-script-help-ad-modules/598246/4","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}},{"@type":"Answer","text":"
It was grabbing info from the .xlsx instead of the .csv.<\/p>\n
Rookie move.<\/p>","upvoteCount":0,"datePublished":"2017-08-07T13:07:18.000Z","url":"https://community.spiceworks.com/t/powershell-script-help-ad-modules/598246/5","author":{"@type":"Person","name":"jacobdurham","url":"https://community.spiceworks.com/u/jacobdurham"}},{"@type":"Answer","text":"
That might be why $group was invalid. But I do not see where you set $group - did you mean $_.group?<\/p>","upvoteCount":0,"datePublished":"2017-08-07T13:09:10.000Z","url":"https://community.spiceworks.com/t/powershell-script-help-ad-modules/598246/6","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}},{"@type":"Answer","text":"