Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3980338
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:16:44+00:00 2026-05-20T05:16:44+00:00

So I’m trying to create a function in powershell that nests all the groups

  • 0

So I’m trying to create a function in powershell that nests all the groups a user is member of in ad. I map the groups into a hashtable, with the common name from AD as the value.

This goes well, and the function returns a hashtable containing all my groups. but when I query it using
$group = "IT-Avd"
$groups.ContainsValue($group)

It returns false — but when I loop over the values and do the same comparison, I do get a match! what’s going on??

function get-groupmemberships ($workDN){
    $ADobj = [ADSI]"LDAP://$workDN"     
    foreach ($currGroup in $ADobj.memberOf.Value){
        if (!($currGroup -eq $null)){
            #Write-Host "displayN:" $grpDetails.Name " | dn:" $currGroup " | "
            if(!$groups.ContainsKey($currGroup)){
                $grpDetails = [ADSI]"LDAP://$currGroup"
                $groups.Add($currGroup,$grpDetails.Name)
                #Write-Host "Adding new pair, " $grpDetails.Name " | " $currGroup
                get-groupmembership $currGroup
            }
        }
    }
    return $groups
}

function get-DN ($SAMToFetch){
    $strFilter = "(samAccountName=$SAMToFetch)"
    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.Filter = $strFilter
    $objPath = $objSearcher.FindOne()
    if($objPath){
        $objFound = $objPath.GetDirectoryEntry()
        $objDN = $objFound.distinguishedName
        return $objDN}
    else{ 
        return $false
    }
}

$groups = @{}   
write-host "Start search:",(Get-Date -Format T),",",(get-Date -format fff)
$group = "IT-Avd"
$memberOf = get-GroupMemberships (get-DN jslonsetteig)
$groups.ContainsValue($group)
foreach( $val in $groups.Values){if ($val -eq $group){Write-Host $val "<--It does seem to be there..."}}

write-host "End search:",(Get-Date -Format T),",",(get-Date -format fff)`
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:16:45+00:00Added an answer on May 20, 2026 at 5:16 am

    Using ContainsValue is using a linear search of the hashtable so you’re losing the benefit of the O(1) search the hashtable normally offers. You might try creating another hashtable where the common name is the key instead. Then you would use ContainsKey or just attempt the access by name and see if the result is not $null.

    BTW one reason for the difference is that when you pass $group to ContainsValue, PowerShell does nothing in the way of type coercion since that method parameter is type object. However when it executes $val -eq $group, PowerShell will potentially do lots of work to coerce $group to $val‘s type. Now while it may appear that $group is a string already, there are some psobject wrapping bugs that may be coming into play here (just a guess though). What happens if you try $groups.ContainsValue('IT-Avd')?

    You might also examine the type stored in the hashtable e.g.:

    $groups.Values | Get-Member
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.