Exchange Powershell Restrict OrganizationalUnit to Top Level OU
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
add a comment |
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
add a comment |
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
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
powershell active-directory exchange-2013
asked Feb 6 at 19:27
Chris LopeChris Lope
11
11
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Feb 7 at 7:42
postanotepostanote
1,053133
1,053133
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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