powershell syntax help

$group1 = "***-***-S-U ZScaler Pilot" 
$Users = get-content 'C:\Powershell Projects\Get-viuser\user.txt'
Foreach ($user in $users) {
        Get-ADUser -identity $user -Properties memberof | where-object {$_.memberOf -notcontains $group1

Open in new window


Can anyone tell me why this doesn't work?

Thanks
Alex
LVL 20
Alex GreenProject Systems EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sajen JoseCommented:
Hi Alex

What is the error you are getting?

you may want to try this line for the Get-ADUser cmdlet.

Get-ADUser -identity $user -Properties memberof | where-object {!($_.memberof -like $group1)}
Alex GreenProject Systems EngineerAuthor Commented:
It was still pulling people with the group added, I want people without the group.
Sajen JoseCommented:
okay... you may want to try the FQDN for the group and give it a shot, remember reading somewhere that the wild chars may have issues...
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Alex GreenProject Systems EngineerAuthor Commented:
I've put it in using the samaccountname too and that doesn't work either :S
Sajen JoseCommented:
Hi Alex,

I tried in my lab environment by creating a group called SJGroup and giving the full DN and searching for the users that belong to this group and it came out fine.

I think in your case you want to exclude the users containing the group, you can probably handle it at the  if condition.

I agree not a pretty script, but it seems to be working at my end :)

$excgroup = "CN=SJGroup,CN=Users,DC=Demo2,DC=LEL"

$mysers = Get-Content "C:\Users\Admin\Desktop\users.txt"

foreach ($user in $myusers)
{
	$userdetails = Get-ADuser -Identity $user -Properties memberOf
	
	foreach ($entry in $userdetails.memberOf)
	{
		If ($excgroup -contains $entry)
		{
			Write-Host $userdetails.SamAccountName
		}
	}
}

Open in new window

Sajen JoseCommented:
Note: $mysers  - the variable name should be $myusers  typo :)
Alex GreenProject Systems EngineerAuthor Commented:
Pretty is irrelevant as long as it works mate,

Trying it now.

Thanks
Alex
Alex GreenProject Systems EngineerAuthor Commented:
Doesn't seem to work, it's giving me users with the group.
footechCommented:
Your original code should work if you supply the distinguishedname for the group.  The -contains and -notcontains operators don't do partial matches against an array/collection.  To get a match your search term has to be present (in totality) as an element of the array.  And since the memberOf attribute is an array of distinguishednames...(gotobeginning)
Alex GreenProject Systems EngineerAuthor Commented:
$group1 = "*something-INT InternetAccessStandard*"
$Users = get-content 'C:\Powershell Projects\Get-viuser\user.txt'
Foreach ($user in $users) {
      Get-ADUser -identity $user -Properties memberof | where-object {!($_.MemberOf -like $group1)} | Select-Object Samaccountname -ExpandProperty Samaccountname}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alex GreenProject Systems EngineerAuthor Commented:
This was the only way to get it to work
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.