Exchange Powershell Restrict OrganizationalUnit to Top Level OU












0















I'm using the following code to check an attribute on mailboxes that reside in an specific OU:



Get-mailbox -OrganizationalUnit "MY-OU" | Select DisplayName, CustomAttribute5 |Export-csv c:filename.csv -nti


I can't find a way to restrict the query to only objects that live in the top level of that OU, which is where the accounts I'm interest in are located. If I run it as it is now, it starts pulling in mailboxes from all of the sub-containers and the job ends after 1,000 results are pulled in. I COULD just remote the default limit, but that would mean I'm pulling down ~10,000 mailboxes vs. the couple of hundred from the top level OU that I actually need to review.



Any ideas on how limit the scope of that request? Other AD limiting parameters don't appear to be valid for Get-mailbox (such as searchbase/searchscope/etc.)



Thank you!










share|improve this question



























    0















    I'm using the following code to check an attribute on mailboxes that reside in an specific OU:



    Get-mailbox -OrganizationalUnit "MY-OU" | Select DisplayName, CustomAttribute5 |Export-csv c:filename.csv -nti


    I can't find a way to restrict the query to only objects that live in the top level of that OU, which is where the accounts I'm interest in are located. If I run it as it is now, it starts pulling in mailboxes from all of the sub-containers and the job ends after 1,000 results are pulled in. I COULD just remote the default limit, but that would mean I'm pulling down ~10,000 mailboxes vs. the couple of hundred from the top level OU that I actually need to review.



    Any ideas on how limit the scope of that request? Other AD limiting parameters don't appear to be valid for Get-mailbox (such as searchbase/searchscope/etc.)



    Thank you!










    share|improve this question

























      0












      0








      0








      I'm using the following code to check an attribute on mailboxes that reside in an specific OU:



      Get-mailbox -OrganizationalUnit "MY-OU" | Select DisplayName, CustomAttribute5 |Export-csv c:filename.csv -nti


      I can't find a way to restrict the query to only objects that live in the top level of that OU, which is where the accounts I'm interest in are located. If I run it as it is now, it starts pulling in mailboxes from all of the sub-containers and the job ends after 1,000 results are pulled in. I COULD just remote the default limit, but that would mean I'm pulling down ~10,000 mailboxes vs. the couple of hundred from the top level OU that I actually need to review.



      Any ideas on how limit the scope of that request? Other AD limiting parameters don't appear to be valid for Get-mailbox (such as searchbase/searchscope/etc.)



      Thank you!










      share|improve this question














      I'm using the following code to check an attribute on mailboxes that reside in an specific OU:



      Get-mailbox -OrganizationalUnit "MY-OU" | Select DisplayName, CustomAttribute5 |Export-csv c:filename.csv -nti


      I can't find a way to restrict the query to only objects that live in the top level of that OU, which is where the accounts I'm interest in are located. If I run it as it is now, it starts pulling in mailboxes from all of the sub-containers and the job ends after 1,000 results are pulled in. I COULD just remote the default limit, but that would mean I'm pulling down ~10,000 mailboxes vs. the couple of hundred from the top level OU that I actually need to review.



      Any ideas on how limit the scope of that request? Other AD limiting parameters don't appear to be valid for Get-mailbox (such as searchbase/searchscope/etc.)



      Thank you!







      powershell active-directory exchange-2013






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 6 at 19:27









      Chris LopeChris Lope

      11




      11






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Why not start with Get-ADuser using a search scope and pipe that to Get-Mailbox



          Get-ADUser -Filter * -SearchBase 'OU=Users,DC=contoso,DC=com' -SearchScope OneLevel | ForEach {
          Get-Mailbox -Identity $_.SamAccountName
          }


          Or...



          You can exclude the ones you don't want using something like this example...
          (tweak as needed)



          # Get all mailboxes from a main OU but exclude a few sub OUs
          # https://community.spiceworks.com/topic/1955950-get-all-mailboxes-from-a-main-ou-but-exclude-a-few-sub-ous

          $TargetOU = 'OU=USA,OU=NNA,DC=NMCorp,DC=Nissan,DC=Biz'

          $OUExclude = @(
          'NMCorp.Nissan.Biz/NNA/USA/US-Terminated Users'
          'NMCorp.Nissan.Biz/NNA/USA/US-Users/Shared Mailboxes'
          'NMCorp.Nissan.Biz/NNA/USA/US-Conference Rooms'
          )

          $CharExclude = @(
          '!'
          '@'
          ','
          )

          $Output = "$PathNNA_Shared_Mailboxes_All_OUs_$( Get-Date -Format MMM.dd.yyyy ).csv"

          $Mailboxes = Get-Mailbox -OrganizationalUnit $TargetOU -ResultSize Unlimited |
          Where-Object {
          $OUExclude -notcontains $_.OrganizationalUnit -and
          $_.DisplayName -notmatch ( $CharExclude -join '|' )
          } |
          Select-Object -Property DisplayName, SamAccountName, RecipientType, RecipientTypeDetails, DistinguishedName,
          @{ Name = "Enabled"; Expression = { ( Get-ADUser -Identity $PSItem.SamAccountName ).Enabled } } |
          Export-Csv -Path $Output -NoTypeInformation -Append





          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            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%2fsuperuser.com%2fquestions%2f1402824%2fexchange-powershell-restrict-organizationalunit-to-top-level-ou%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














            Why not start with Get-ADuser using a search scope and pipe that to Get-Mailbox



            Get-ADUser -Filter * -SearchBase 'OU=Users,DC=contoso,DC=com' -SearchScope OneLevel | ForEach {
            Get-Mailbox -Identity $_.SamAccountName
            }


            Or...



            You can exclude the ones you don't want using something like this example...
            (tweak as needed)



            # Get all mailboxes from a main OU but exclude a few sub OUs
            # https://community.spiceworks.com/topic/1955950-get-all-mailboxes-from-a-main-ou-but-exclude-a-few-sub-ous

            $TargetOU = 'OU=USA,OU=NNA,DC=NMCorp,DC=Nissan,DC=Biz'

            $OUExclude = @(
            'NMCorp.Nissan.Biz/NNA/USA/US-Terminated Users'
            'NMCorp.Nissan.Biz/NNA/USA/US-Users/Shared Mailboxes'
            'NMCorp.Nissan.Biz/NNA/USA/US-Conference Rooms'
            )

            $CharExclude = @(
            '!'
            '@'
            ','
            )

            $Output = "$PathNNA_Shared_Mailboxes_All_OUs_$( Get-Date -Format MMM.dd.yyyy ).csv"

            $Mailboxes = Get-Mailbox -OrganizationalUnit $TargetOU -ResultSize Unlimited |
            Where-Object {
            $OUExclude -notcontains $_.OrganizationalUnit -and
            $_.DisplayName -notmatch ( $CharExclude -join '|' )
            } |
            Select-Object -Property DisplayName, SamAccountName, RecipientType, RecipientTypeDetails, DistinguishedName,
            @{ Name = "Enabled"; Expression = { ( Get-ADUser -Identity $PSItem.SamAccountName ).Enabled } } |
            Export-Csv -Path $Output -NoTypeInformation -Append





            share|improve this answer




























              0














              Why not start with Get-ADuser using a search scope and pipe that to Get-Mailbox



              Get-ADUser -Filter * -SearchBase 'OU=Users,DC=contoso,DC=com' -SearchScope OneLevel | ForEach {
              Get-Mailbox -Identity $_.SamAccountName
              }


              Or...



              You can exclude the ones you don't want using something like this example...
              (tweak as needed)



              # Get all mailboxes from a main OU but exclude a few sub OUs
              # https://community.spiceworks.com/topic/1955950-get-all-mailboxes-from-a-main-ou-but-exclude-a-few-sub-ous

              $TargetOU = 'OU=USA,OU=NNA,DC=NMCorp,DC=Nissan,DC=Biz'

              $OUExclude = @(
              'NMCorp.Nissan.Biz/NNA/USA/US-Terminated Users'
              'NMCorp.Nissan.Biz/NNA/USA/US-Users/Shared Mailboxes'
              'NMCorp.Nissan.Biz/NNA/USA/US-Conference Rooms'
              )

              $CharExclude = @(
              '!'
              '@'
              ','
              )

              $Output = "$PathNNA_Shared_Mailboxes_All_OUs_$( Get-Date -Format MMM.dd.yyyy ).csv"

              $Mailboxes = Get-Mailbox -OrganizationalUnit $TargetOU -ResultSize Unlimited |
              Where-Object {
              $OUExclude -notcontains $_.OrganizationalUnit -and
              $_.DisplayName -notmatch ( $CharExclude -join '|' )
              } |
              Select-Object -Property DisplayName, SamAccountName, RecipientType, RecipientTypeDetails, DistinguishedName,
              @{ Name = "Enabled"; Expression = { ( Get-ADUser -Identity $PSItem.SamAccountName ).Enabled } } |
              Export-Csv -Path $Output -NoTypeInformation -Append





              share|improve this answer


























                0












                0








                0







                Why not start with Get-ADuser using a search scope and pipe that to Get-Mailbox



                Get-ADUser -Filter * -SearchBase 'OU=Users,DC=contoso,DC=com' -SearchScope OneLevel | ForEach {
                Get-Mailbox -Identity $_.SamAccountName
                }


                Or...



                You can exclude the ones you don't want using something like this example...
                (tweak as needed)



                # Get all mailboxes from a main OU but exclude a few sub OUs
                # https://community.spiceworks.com/topic/1955950-get-all-mailboxes-from-a-main-ou-but-exclude-a-few-sub-ous

                $TargetOU = 'OU=USA,OU=NNA,DC=NMCorp,DC=Nissan,DC=Biz'

                $OUExclude = @(
                'NMCorp.Nissan.Biz/NNA/USA/US-Terminated Users'
                'NMCorp.Nissan.Biz/NNA/USA/US-Users/Shared Mailboxes'
                'NMCorp.Nissan.Biz/NNA/USA/US-Conference Rooms'
                )

                $CharExclude = @(
                '!'
                '@'
                ','
                )

                $Output = "$PathNNA_Shared_Mailboxes_All_OUs_$( Get-Date -Format MMM.dd.yyyy ).csv"

                $Mailboxes = Get-Mailbox -OrganizationalUnit $TargetOU -ResultSize Unlimited |
                Where-Object {
                $OUExclude -notcontains $_.OrganizationalUnit -and
                $_.DisplayName -notmatch ( $CharExclude -join '|' )
                } |
                Select-Object -Property DisplayName, SamAccountName, RecipientType, RecipientTypeDetails, DistinguishedName,
                @{ Name = "Enabled"; Expression = { ( Get-ADUser -Identity $PSItem.SamAccountName ).Enabled } } |
                Export-Csv -Path $Output -NoTypeInformation -Append





                share|improve this answer













                Why not start with Get-ADuser using a search scope and pipe that to Get-Mailbox



                Get-ADUser -Filter * -SearchBase 'OU=Users,DC=contoso,DC=com' -SearchScope OneLevel | ForEach {
                Get-Mailbox -Identity $_.SamAccountName
                }


                Or...



                You can exclude the ones you don't want using something like this example...
                (tweak as needed)



                # Get all mailboxes from a main OU but exclude a few sub OUs
                # https://community.spiceworks.com/topic/1955950-get-all-mailboxes-from-a-main-ou-but-exclude-a-few-sub-ous

                $TargetOU = 'OU=USA,OU=NNA,DC=NMCorp,DC=Nissan,DC=Biz'

                $OUExclude = @(
                'NMCorp.Nissan.Biz/NNA/USA/US-Terminated Users'
                'NMCorp.Nissan.Biz/NNA/USA/US-Users/Shared Mailboxes'
                'NMCorp.Nissan.Biz/NNA/USA/US-Conference Rooms'
                )

                $CharExclude = @(
                '!'
                '@'
                ','
                )

                $Output = "$PathNNA_Shared_Mailboxes_All_OUs_$( Get-Date -Format MMM.dd.yyyy ).csv"

                $Mailboxes = Get-Mailbox -OrganizationalUnit $TargetOU -ResultSize Unlimited |
                Where-Object {
                $OUExclude -notcontains $_.OrganizationalUnit -and
                $_.DisplayName -notmatch ( $CharExclude -join '|' )
                } |
                Select-Object -Property DisplayName, SamAccountName, RecipientType, RecipientTypeDetails, DistinguishedName,
                @{ Name = "Enabled"; Expression = { ( Get-ADUser -Identity $PSItem.SamAccountName ).Enabled } } |
                Export-Csv -Path $Output -NoTypeInformation -Append






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 7 at 7:42









                postanotepostanote

                1,053133




                1,053133






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • 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%2fsuperuser.com%2fquestions%2f1402824%2fexchange-powershell-restrict-organizationalunit-to-top-level-ou%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

                    Mouse cursor on multiple screens with different PPI

                    Agildo Ribeiro

                    Sometime when accessing a menu: “Ubuntu 16.04 has experienced an internal error”