question

MohamedSAKHO-2817 avatar image
0 Votes"
MohamedSAKHO-2817 asked StoyanChalakov edited

How to retrieve all empty OU in all my AD

Hi all

I would like to know all empty OU existing on my AD.
I found a powershell command line below which retrieve this information:

Get-ADOrganizationalUnit -Filter | ForEach-Object {
if (-not (Get-ADObject -SearchBase $_ -SearchScope OneLevel -Filter
))
{
$.Name +";;;;;;;;;;;;;;;;;;;;;;;;;"+$.DistinguishedName
}
}

This command works but it's not optimal. for example I would like to export to csv file with "export-csv -path filename.csv" and it doesn't work.
Idon't feel comfortable with powershell..
Does someone can help me to improve this script and export it. Or maybe somebody has something better in what I need?
Thank you by advance.

Mohamed

windows-server-powershellwindows-active-directory
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

KlausBilger-5062 avatar image
0 Votes"
KlausBilger-5062 answered

i guess you can try this

Get-ADOrganizationalUnit -Filter |
Where-Object {-not ( Get-ADObject -Filter
-SearchBase $_.Distinguishedname -SearchScope OneLevel -ResultSetSize 1 )}

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YoungYang-MSFT avatar image
0 Votes"
YoungYang-MSFT answered

Hi, given that this post has been quiet for a while, this is a quick question and answer. Has your question been solved? If so, please mark it as an answer so that users with the same question can find and get help.
:)

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SAKHOMohamedExterne-1703 avatar image
0 Votes"
SAKHOMohamedExterne-1703 answered

Hi,

sorry for the late I was in holidays, that's why I didn't answered.
I tryied the command line but it doesn't work:


19142-image.png
19059-2020-08-20-11-35.png



Thank you by advance.


image.png (103.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

StoyanChalakov avatar image
1 Vote"
StoyanChalakov answered StoyanChalakov edited

Hi @SAKHOMohamedExterne-1703,

This can be easily solved, you have missed a "*" in front of "-Filter" (2x). Please try it like that:

 Get-ADOrganizationalUnit -Filter * | Where-Object {-not ( Get-ADObject -Filter * -SearchBase $_.Distinguishedname -SearchScope OneLevel -ResultSetSize 1 )}

This will help you find the empty OUs.

Regards


(If the reply was helpful please don't forget to upvote or accept as answer, thank you)

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.