A problem is that a computer can belong to more than one OU (through nesting), and also to a container (which is a cn=, not an ou=, like the built-in Computers container).
I should also point out that you’re repeatedly querying all the OUs from AD, which isn’t very efficient and could load the domain controller. It’d perhaps be better to query the OUs one time, and then enumerate them to look for matches against your list.
A one-liner isn’t going to be the most efficient or easy-to-write way to do this - rather than chaining commands, it might be better to write a more formal script.
If you could outline the business goal here, it might be easier for me to understand what you’re trying to do, and to offer a useful suggestion?
I am supposed to query our AD structure to find specific OU’s with a given name. Based on the DN of that search, I do not want to see certain OU’s with another set of names. Then I want to get a count of all the computer objects in the OU’s based on the previous results. I was gonna use the Group-Object cmdlet to do this. When I group it, I see the CN’s of the DN’s. I just want to see the OU path.
EDIT:
I should also point out that this will only work for computer accounts. While at work I don’t have access to my old jobs repo of stuff I wrote - I know I got something in there that would handle user accounts as well but that would have to wait until after work but if your only looking for computer you should be fine.