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


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

One thought on “Writing out computer list retrieved from Active Directory and output to a text file using Add-Content”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: