none
System.Collections.ArrayList .contains method RRS feed

  • Question

  • I'm trying to create an everyone but, these people group using PowerShell.

    $Users=[System.Collections.ArrayList]@()

    $DeniedUsers=[system.Collections.ArrayList](Get-ADGroupMember"ADSecGroup"
    | Select DistinguishedName)

    Get-ADUser
    -Filter*-SearchBase"OU=USERS,DC=CORP,DC=COM" | Select DistinguishedName
    | ForEach-Object{$null=$Users.add($_)}

    I want a new collection that contains everyone in $Users except those in $DeniedUsers. I've been trying things like this without any success.

    $AllowedUsers=$Users | Where-Object{!$_.Contains($DeniedUsers.DistinguishedName)}


     


    Robert W. Kirchhof






    • Edited by RKirchhof Wednesday, July 6, 2016 3:12 PM
    Wednesday, July 6, 2016 3:02 PM

Answers

  • Here's a short example of one way to get a list of items that does not contain items from another list.


    $all = "A","B","C","D","E"
    $exempt = "B","C"
    $all | ForEach-Object {
      if ( $exempt -notcontains $_ ) {
        $_
      }
    }
    # Outputs list "A","D","E"
    


    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 6, 2016 3:08 PM
    Moderator

All replies

  • Here's a short example of one way to get a list of items that does not contain items from another list.


    $all = "A","B","C","D","E"
    $exempt = "B","C"
    $all | ForEach-Object {
      if ( $exempt -notcontains $_ ) {
        $_
      }
    }
    # Outputs list "A","D","E"
    


    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 6, 2016 3:08 PM
    Moderator
  • Please don't post colorized code.  It is badly broken when pasted.

    $DeniedUsers = Get-ADGroupMember ADSecGroup	| Select  -expand DistinguishedName
    Get-ADUser -Filter * -SearchBase 'OU=USERS,DC=CORP,DC=COM' | 
    	Where{ $_.Distinguished -notin $DeniedUsers}
    

    Use this code posting tool.


    \_(ツ)_/

    Wednesday, July 6, 2016 3:25 PM