Export Domain Computers with Powershell

January 7, 2011

If you want to export all domain computers to a csv file , it’s very humble with powershell.

Let’s see step by step.

$strFilter = "(&(objectCategory=Computer))"

First of all, you will attach computer filter for $strFilter variable.
 

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

Connect to Active Directory Searcher.

$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"

Just export 1000 line and specify the SearchScope property.

Searchroot property tells DirectorySearcher where to begin.If you want to search entire domain we set the reference $objDomain .
 

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

Property lists need to be constructed as arrays.In powershell you can create an array by assigning multiple values.Very simple.

$colResults = $objSearcher.FindAll()

We call FindAll() method to to search active directory.

$colResults | %{ $a = New-Object PSObject ; $a | Add-Member -MemberType NoteProperty -Name Name -Value $_.Properties.Item("Name")[0] ; $a } | Export-Csv -NoTypeInformation "C:\server\ExportComputers.csv"

At least, for each item we must store the item properties in a variable.Objcomputer.name give us an output, take that and export as an csv file.

"WIN-1TVR4JD86GE"
"HUBCAS"
"MBX1"
"MBX2"
"DAG1"
"SCCM1"

posted in Powershell by Anıl ERDURAN

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org