Writing out computer list retrieved from Active Directory and output to a text file using Add-Content

Tags: powershell

Hope this helps someone, it took me a bit to get the syntax down and understand why. (Yes, I'm still a PoSh newbie) I discovered when I was trying to write out a computer list retrieved from Active Directory, the output in the file was System.DirectoryServices.ResultPropertyValueCollection.   After I added explicitly [String] $a in the code below, when the value was added to the file I specified, the actual ASCII value showed up.  thought I would pass this alongQ

$strCategory = "computer"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher

$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = ("(objectCategory=$strCategory)")

$colProplist = "name"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults)

{
 $objComputer = $objResult.Properties; $objComputer.name
 [String]$a = $objComputer.name
 Add-Content ss.txt $a
}


Cheers,

Steve Schofield

1 Comment

Add a Comment