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 16
Alex Green3rd Line Server SupportAsked:
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)}
0
Alex Green3rd Line Server SupportAuthor Commented:
It was still pulling people with the group added, I want people without the group.
0
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...
0
Top Threats of Q1 & How to Defend Against Them

WEBINAR: Join WatchGuard CTO and our Threat Research Team on Aug. 2nd to hear the findings from our Q1 Internet Security Report! Learn more about the top threats detected in the first quarter and how you can defend your business against them!

Alex Green3rd Line Server SupportAuthor Commented:
I've put it in using the samaccountname too and that doesn't work either :S
0
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

0
Sajen JoseCommented:
Note: $mysers  - the variable name should be $myusers  typo :)
0
Alex Green3rd Line Server SupportAuthor Commented:
Pretty is irrelevant as long as it works mate,

Trying it now.

Thanks
Alex
0
Alex Green3rd Line Server SupportAuthor Commented:
Doesn't seem to work, it's giving me users with the group.
0
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)
0
Alex Green3rd Line Server SupportAuthor 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}
0

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 Green3rd Line Server SupportAuthor Commented:
This was the only way to get it to work
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.