Powershell Compare-Object compare ad users not in list of groups












0















I'm trying to find all users that have mailboxes, but are not in some expected groups (where we put our users we expect to have mailboxes). I've gotten it working in my script, but the way I did it is to loop through the collection of user accounts and verify their memberships individually.....which is excruciatingly slow. I'm trying to instead compare the (sorted) list of user accounts to the (sorted unique list) of those in the member groups....but not having success with the compare-object command.



$GrpMembersUnique is the sorted list of unique accounts in the various groups



$GrpMembersUnique = Get-AdGroup -SearchBase "OU=Groups,DC=blahblah" -filter {name -eq 'GRP_STAFF' -OR name -eq 'GRP_2' ....} -properties member | select-object -ExpandProperty member | get-AdUser -properties SamAccountName | Sort-Object | Get-Unique


$userlist is the sorted list of unique accounts with mailboxes



$userlist=Get-aduser -SearchBase "OU=People,DC=blahblah" -filter {mail -like "*" } -properties mail,SamAccountName | Sort-Object | Get-Unique


Compare command:



$ChecklList = compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -ExcludeDifferent -Property 'SamAccountName' | Where {$_.SideIndicator -like "<="} 


returns no results. this should be simple, so I'm thinking I missed something easy....any help or guidance appreciated.










share|improve this question

























  • your command is excluding all those that are not in both. [grin] look at the output of this Get-Help Compare-Object -Parameter ExcludeDifferent and note Indicates that this cmdlet displays only the characteristics of compared objects that are equal.

    – Lee_Dailey
    Nov 21 '18 at 17:31











  • Can you paste some sample data of the $userlist and $GrpMembersUnique in the question?

    – iRon
    Nov 21 '18 at 17:38











  • Sure. I'll add to the original post so I can format better.....

    – Jeff
    Nov 21 '18 at 17:40













  • Thanks Lee. I did try without the -ExcludeDifferent switch....but got the same results (should posted that earlier). Meaning, tried the command as such with no luck: $ChecklList = compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -Property SamAccountName | Where {$_.SideIndicator -like "<="}

    – Jeff
    Nov 21 '18 at 17:48











  • Thanks again Lee. That hint steered me in the right direction. added the -IncludeEqual switch instead, and works now. full command now reads: compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -IncludeEqual -Property 'SamAccountName' | Where {$_.SideIndicator -like "<="}

    – Jeff
    Nov 21 '18 at 17:55
















0















I'm trying to find all users that have mailboxes, but are not in some expected groups (where we put our users we expect to have mailboxes). I've gotten it working in my script, but the way I did it is to loop through the collection of user accounts and verify their memberships individually.....which is excruciatingly slow. I'm trying to instead compare the (sorted) list of user accounts to the (sorted unique list) of those in the member groups....but not having success with the compare-object command.



$GrpMembersUnique is the sorted list of unique accounts in the various groups



$GrpMembersUnique = Get-AdGroup -SearchBase "OU=Groups,DC=blahblah" -filter {name -eq 'GRP_STAFF' -OR name -eq 'GRP_2' ....} -properties member | select-object -ExpandProperty member | get-AdUser -properties SamAccountName | Sort-Object | Get-Unique


$userlist is the sorted list of unique accounts with mailboxes



$userlist=Get-aduser -SearchBase "OU=People,DC=blahblah" -filter {mail -like "*" } -properties mail,SamAccountName | Sort-Object | Get-Unique


Compare command:



$ChecklList = compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -ExcludeDifferent -Property 'SamAccountName' | Where {$_.SideIndicator -like "<="} 


returns no results. this should be simple, so I'm thinking I missed something easy....any help or guidance appreciated.










share|improve this question

























  • your command is excluding all those that are not in both. [grin] look at the output of this Get-Help Compare-Object -Parameter ExcludeDifferent and note Indicates that this cmdlet displays only the characteristics of compared objects that are equal.

    – Lee_Dailey
    Nov 21 '18 at 17:31











  • Can you paste some sample data of the $userlist and $GrpMembersUnique in the question?

    – iRon
    Nov 21 '18 at 17:38











  • Sure. I'll add to the original post so I can format better.....

    – Jeff
    Nov 21 '18 at 17:40













  • Thanks Lee. I did try without the -ExcludeDifferent switch....but got the same results (should posted that earlier). Meaning, tried the command as such with no luck: $ChecklList = compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -Property SamAccountName | Where {$_.SideIndicator -like "<="}

    – Jeff
    Nov 21 '18 at 17:48











  • Thanks again Lee. That hint steered me in the right direction. added the -IncludeEqual switch instead, and works now. full command now reads: compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -IncludeEqual -Property 'SamAccountName' | Where {$_.SideIndicator -like "<="}

    – Jeff
    Nov 21 '18 at 17:55














0












0








0








I'm trying to find all users that have mailboxes, but are not in some expected groups (where we put our users we expect to have mailboxes). I've gotten it working in my script, but the way I did it is to loop through the collection of user accounts and verify their memberships individually.....which is excruciatingly slow. I'm trying to instead compare the (sorted) list of user accounts to the (sorted unique list) of those in the member groups....but not having success with the compare-object command.



$GrpMembersUnique is the sorted list of unique accounts in the various groups



$GrpMembersUnique = Get-AdGroup -SearchBase "OU=Groups,DC=blahblah" -filter {name -eq 'GRP_STAFF' -OR name -eq 'GRP_2' ....} -properties member | select-object -ExpandProperty member | get-AdUser -properties SamAccountName | Sort-Object | Get-Unique


$userlist is the sorted list of unique accounts with mailboxes



$userlist=Get-aduser -SearchBase "OU=People,DC=blahblah" -filter {mail -like "*" } -properties mail,SamAccountName | Sort-Object | Get-Unique


Compare command:



$ChecklList = compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -ExcludeDifferent -Property 'SamAccountName' | Where {$_.SideIndicator -like "<="} 


returns no results. this should be simple, so I'm thinking I missed something easy....any help or guidance appreciated.










share|improve this question
















I'm trying to find all users that have mailboxes, but are not in some expected groups (where we put our users we expect to have mailboxes). I've gotten it working in my script, but the way I did it is to loop through the collection of user accounts and verify their memberships individually.....which is excruciatingly slow. I'm trying to instead compare the (sorted) list of user accounts to the (sorted unique list) of those in the member groups....but not having success with the compare-object command.



$GrpMembersUnique is the sorted list of unique accounts in the various groups



$GrpMembersUnique = Get-AdGroup -SearchBase "OU=Groups,DC=blahblah" -filter {name -eq 'GRP_STAFF' -OR name -eq 'GRP_2' ....} -properties member | select-object -ExpandProperty member | get-AdUser -properties SamAccountName | Sort-Object | Get-Unique


$userlist is the sorted list of unique accounts with mailboxes



$userlist=Get-aduser -SearchBase "OU=People,DC=blahblah" -filter {mail -like "*" } -properties mail,SamAccountName | Sort-Object | Get-Unique


Compare command:



$ChecklList = compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -ExcludeDifferent -Property 'SamAccountName' | Where {$_.SideIndicator -like "<="} 


returns no results. this should be simple, so I'm thinking I missed something easy....any help or guidance appreciated.







powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 17:46







Jeff

















asked Nov 21 '18 at 17:11









JeffJeff

3519




3519













  • your command is excluding all those that are not in both. [grin] look at the output of this Get-Help Compare-Object -Parameter ExcludeDifferent and note Indicates that this cmdlet displays only the characteristics of compared objects that are equal.

    – Lee_Dailey
    Nov 21 '18 at 17:31











  • Can you paste some sample data of the $userlist and $GrpMembersUnique in the question?

    – iRon
    Nov 21 '18 at 17:38











  • Sure. I'll add to the original post so I can format better.....

    – Jeff
    Nov 21 '18 at 17:40













  • Thanks Lee. I did try without the -ExcludeDifferent switch....but got the same results (should posted that earlier). Meaning, tried the command as such with no luck: $ChecklList = compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -Property SamAccountName | Where {$_.SideIndicator -like "<="}

    – Jeff
    Nov 21 '18 at 17:48











  • Thanks again Lee. That hint steered me in the right direction. added the -IncludeEqual switch instead, and works now. full command now reads: compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -IncludeEqual -Property 'SamAccountName' | Where {$_.SideIndicator -like "<="}

    – Jeff
    Nov 21 '18 at 17:55



















  • your command is excluding all those that are not in both. [grin] look at the output of this Get-Help Compare-Object -Parameter ExcludeDifferent and note Indicates that this cmdlet displays only the characteristics of compared objects that are equal.

    – Lee_Dailey
    Nov 21 '18 at 17:31











  • Can you paste some sample data of the $userlist and $GrpMembersUnique in the question?

    – iRon
    Nov 21 '18 at 17:38











  • Sure. I'll add to the original post so I can format better.....

    – Jeff
    Nov 21 '18 at 17:40













  • Thanks Lee. I did try without the -ExcludeDifferent switch....but got the same results (should posted that earlier). Meaning, tried the command as such with no luck: $ChecklList = compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -Property SamAccountName | Where {$_.SideIndicator -like "<="}

    – Jeff
    Nov 21 '18 at 17:48











  • Thanks again Lee. That hint steered me in the right direction. added the -IncludeEqual switch instead, and works now. full command now reads: compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -IncludeEqual -Property 'SamAccountName' | Where {$_.SideIndicator -like "<="}

    – Jeff
    Nov 21 '18 at 17:55

















your command is excluding all those that are not in both. [grin] look at the output of this Get-Help Compare-Object -Parameter ExcludeDifferent and note Indicates that this cmdlet displays only the characteristics of compared objects that are equal.

– Lee_Dailey
Nov 21 '18 at 17:31





your command is excluding all those that are not in both. [grin] look at the output of this Get-Help Compare-Object -Parameter ExcludeDifferent and note Indicates that this cmdlet displays only the characteristics of compared objects that are equal.

– Lee_Dailey
Nov 21 '18 at 17:31













Can you paste some sample data of the $userlist and $GrpMembersUnique in the question?

– iRon
Nov 21 '18 at 17:38





Can you paste some sample data of the $userlist and $GrpMembersUnique in the question?

– iRon
Nov 21 '18 at 17:38













Sure. I'll add to the original post so I can format better.....

– Jeff
Nov 21 '18 at 17:40







Sure. I'll add to the original post so I can format better.....

– Jeff
Nov 21 '18 at 17:40















Thanks Lee. I did try without the -ExcludeDifferent switch....but got the same results (should posted that earlier). Meaning, tried the command as such with no luck: $ChecklList = compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -Property SamAccountName | Where {$_.SideIndicator -like "<="}

– Jeff
Nov 21 '18 at 17:48





Thanks Lee. I did try without the -ExcludeDifferent switch....but got the same results (should posted that earlier). Meaning, tried the command as such with no luck: $ChecklList = compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -Property SamAccountName | Where {$_.SideIndicator -like "<="}

– Jeff
Nov 21 '18 at 17:48













Thanks again Lee. That hint steered me in the right direction. added the -IncludeEqual switch instead, and works now. full command now reads: compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -IncludeEqual -Property 'SamAccountName' | Where {$_.SideIndicator -like "<="}

– Jeff
Nov 21 '18 at 17:55





Thanks again Lee. That hint steered me in the right direction. added the -IncludeEqual switch instead, and works now. full command now reads: compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -IncludeEqual -Property 'SamAccountName' | Where {$_.SideIndicator -like "<="}

– Jeff
Nov 21 '18 at 17:55












1 Answer
1






active

oldest

votes


















0














Using wrong switch. Changed -ExcludeDifferent to -IncludeEqual, and working as expected now. Thanks Lee_Dailey.



compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -IncludeEqual -Property 'SamAccountName' | Where {$_.SideIndicator -like "<="} 





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417319%2fpowershell-compare-object-compare-ad-users-not-in-list-of-groups%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Using wrong switch. Changed -ExcludeDifferent to -IncludeEqual, and working as expected now. Thanks Lee_Dailey.



    compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -IncludeEqual -Property 'SamAccountName' | Where {$_.SideIndicator -like "<="} 





    share|improve this answer




























      0














      Using wrong switch. Changed -ExcludeDifferent to -IncludeEqual, and working as expected now. Thanks Lee_Dailey.



      compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -IncludeEqual -Property 'SamAccountName' | Where {$_.SideIndicator -like "<="} 





      share|improve this answer


























        0












        0








        0







        Using wrong switch. Changed -ExcludeDifferent to -IncludeEqual, and working as expected now. Thanks Lee_Dailey.



        compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -IncludeEqual -Property 'SamAccountName' | Where {$_.SideIndicator -like "<="} 





        share|improve this answer













        Using wrong switch. Changed -ExcludeDifferent to -IncludeEqual, and working as expected now. Thanks Lee_Dailey.



        compare-object -ReferenceObject $userlist -DifferenceObject $GrpMembersUnique -IncludeEqual -Property 'SamAccountName' | Where {$_.SideIndicator -like "<="} 






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 17:57









        JeffJeff

        3519




        3519
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417319%2fpowershell-compare-object-compare-ad-users-not-in-list-of-groups%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            Npm cannot find a required file even through it is in the searched directory