powershell - Export an array with custom objects -


basically, i'm trying do, retrieve users active directory , save them in .csv file, using powershell script. in addition, want attributes "name" , "samaccountname" listed. here's code:

$strfilter = "somefilter" $objcollection = @()  $objdomain = new-object system.directoryservices.directoryentry  $objsearcher = new-object system.directoryservices.directorysearcher $objsearcher.searchroot = $objdomain $objsearcher.pagesize = 1000 $objsearcher.filter = $strfilter $objsearcher.searchscope = "subtree"  $colproplist = "name", "samaccountname" foreach ($i in $colproplist){$objsearcher.propertiestoload.add($i)}  $colresults = $objsearcher.findall()  foreach ($objresult in $colresults) {   $objitem = $objresult.properties    $object = new-object psobject   $object | add-member -membertype noteproperty -name name -value $objitem.name   $object | add-member -membertype noteproperty -name samaccountname -value $objitem.samaccountname    $objcollection+=$object }  $objcollection # gives me output wished $objcollection | export-csv -notypeinformation -path c:\temp\exportfile.csv # doesn't work 

the console output looks this:

name                                samaccountname ----                                -------------- {iusr_pftt-dc1}                     {iusr_pftt-dc1} {iusr_pfvm-dc1}                     {iusr_pfvm-dc1} {iusr_pfxx-dc1}                     {iusr_pfxx-dc1} 

but exported .csv looks this:

"name","samaccountname" "system.directoryservices.resultpropertyvaluecollection","system.directoryservices.resultpropertyvaluecollection" "system.directoryservices.resultpropertyvaluecollection","system.directoryservices.resultpropertyvaluecollection" "system.directoryservices.resultpropertyvaluecollection","system.directoryservices.resultpropertyvaluecollection" "system.directoryservices.resultpropertyvaluecollection","system.directoryservices.resultpropertyvaluecollection" 

any ideas/solutions this?

you can user on domain using active directory module:

import-module activedirectory get-aduser -filter * | select name,samaccountname | convertto-csv | ac "c:\yourcsvfile.csv" 

this give output of users.

"name","samaccountname" "matthe_g","matthe_g" "putine_i","putine_i" "cobb_c","cobb_c" "bull_t","bull_t" "bayol_b","bayol_b" "cappon_p","cappon_p" .... 

note: need turn on active directory windows feature able use activedirectory module. can found under 'remote sever administration tools' tab in windows features.

link: for cmdlet's in ad module


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -