locked
Outputting if a user is in a group -- How to make this script faster? RRS feed

  • Question

  • $group = "Test Group"
    $member1 = Get-ADGroupMember -server SERVER1 -Identity $group -Recursive | Select -ExpandProperty SAMAccountName
    $member2 = Get-ADGroupMember -server SERVER2 -Identity $group -Recursive | Select -ExpandProperty SAMAccountName
    $member3 = Get-ADGroupMember -server SERVER3 -Identity $group -Recursive | Select -ExpandProperty SAMAccountName
    $member4 = Get-ADGroupMember -server SERVER4 -Identity $group -Recursive | Select -ExpandProperty SAMAccountName
    $users = ForEach ($user in $(Get-Content .\csv_file.csv)) {


        If ($member1 -contains $user) {
            Write-Host "`nSERVER1: `n$user in group"
        } 
        ElseIf ($member2 -contains $user) {
            Write-Host "`nSERVER2: `n$user in group"
        }
        ElseIf ($member3 -contains $user) {
            Write-Host "`nSERVER3: `n$user in group"
        }
        ElseIf ($member4 -contains $user) {
            Write-Host "`nSERVER4: `n$user in group"
        }

    Is there any way to make this script faster? It usually takes several minutes to output all the names that are in the group. (Also depending on the .csv size).

    Thursday, June 7, 2018 10:52 PM

All replies

  • Why are you querying 4 different servers for the same group? If you put all found sAMAccountNames in one object and the sAMAccoutnNames from your csv file in another object you can use Compare-Object to compare these two objects. ;-)

    And please, format your code as code here in the forum. Thanks.


    Best regards,

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Friday, June 8, 2018 12:27 AM
    Friday, June 8, 2018 12:26 AM
  • I suspect the servers are in different domains. Using the GC could improve performance.


    \_(ツ)_/

    Friday, June 8, 2018 12:31 AM