Get OU from List of Computers

I have a file that contains several hundred computer names that I want to be able to get the distinguished name. I think I'm close but just not close enough.
Here's what I am using:
Get-Content .\servers.txt | Foreach-Object{
 Get-ADComputer $_
} | Select-Object name,@{
} | Export-CSV -path server.csv -NoTypeInformation

I can get a lot of the DN's from this script, but there are a lot that come back with ObjectNotFound. If I run Get-ADComputer manually on the one that had the error, it shows fine.
here's the error I get on some computers:

Get-ADComputer : Cannot find an object with identity: 'servername ' under: 'DC=domain,DC=local'.
At C:\tools\scripts\get-OU3.ps1:2 char:2
+  Get-ADComputer $_
+  ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (servername :ADComputer) [Get-ADComputer], ADIdentityNotFoundExcepti
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,M

Any thoughts on why this is not working completely? or maybe my script isn't complete? Any help is much appreciated.
Chris WegerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subash SundharanIT Infrastructure Architect Commented:
Your computer names in input file may contain trailing space..
Get-Content .\servers.txt | Foreach-Object{
  Get-ADComputer $_.Trim()
  } | Select-Object name,@{
 } | Export-CSV -path server.csv -NoTypeInformation

Open in new window

You can also use the CanonicalName property to get the OU information..
For example..
Get-Content .\servers.txt | Foreach-Object{
  Get-ADComputer $_.Trim() -pr CanonicalName
  } | Select-Object name,@{
  $_.CanonicalName -Replace "/$($_.Name)"
 } | Export-CSV -path server.csv -NoTypeInformation

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
There is a trailing space in 'servername '.
Chris WegerAuthor Commented:
Yep, you all were right. trailing spaces in the file.

thanks for the canonical name solution too! I like that one better than mine!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.