Thursday, August 2, 2012

A Useful way to export a list of servers from AD

I was trying to come up with a list of all my servers from AD and write it to a text file.  Sounds like it should be a slam-dunk, right?  Get-ADComputer with some parameters and pipe it to out-file.  Look at the file in notepad, and it looks perfect.....and then I try to use that file as the input for another script.  Guess what?  the server names are all padded with spaces at the end of them, which causes all kinds of errors.


This works much better:
PS C:\temp> Get-ADComputer -Filter {OperatingSystem -Like "*server*"}  -Property Name | foreach {$_.Name.Trim()} |out-file filename.txt

No comments:

Post a Comment