Export an array with custom objects
Asked Answered
E

2

6

Basically, what I'm trying to do, is to retrieve all users from Active Directory and to save them in a .csv file, using a PowerShell script. In addition, I only want the attributes "name" and "samaccountname" to be listed. So here's the 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 # this gives me the output as wished
$objCollection | Export-CSV -NoTypeInformation -Path C:\temp\exportfile.csv # this doesn't work

The Console Output looks like this:

Name                                SAMAccountname
----                                --------------
{IUSR_PFTT-DC1}                     {IUSR_PFTT-DC1}
{IUSR_PFVM-DC1}                     {IUSR_PFVM-DC1}
{IUSR_PFXX-DC1}                     {IUSR_PFXX-DC1}

But the exported .csv looks like 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 to this?

Eatage answered 8/8, 2013 at 10:33 Comment(0)
S
2

You can get all the user on your domain by using the Active Directory module:

import-module activedirectory
get-ADuser -filter * | select name,SamAccountName | ConvertTo-CSV | ac "C:\yourCSVFile.csv"

This will give you an output like this of all your 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: You will need to turn on the active directory windows feature to be able to use the activedirectory module. This can be found under 'Remote Sever Administration tools' tab in windows features.

Link: For Cmdlet's in the AD module

Soapsuds answered 8/8, 2013 at 10:56 Comment(1)
I can't believe that I didn't stumble across this, while searching the internet for a solution... Thank you very much.Eatage
G
7

If you want to stick with the DirectorySearcher approach, change this:

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
    }

into this:

$objCollection = $colResults | select -Expand Properties |
    select @{n='Name';e={$_.name}}, @{n='SAMAccountName';e={$_.samaccountname}}
Giverin answered 8/8, 2013 at 12:3 Comment(1)
@AnsgarWiechers, is there a way to convert the property's Value from a ResultPropertyValueCollection to a string, rather than having to list each property individually (as you've done)?Transducer
S
2

You can get all the user on your domain by using the Active Directory module:

import-module activedirectory
get-ADuser -filter * | select name,SamAccountName | ConvertTo-CSV | ac "C:\yourCSVFile.csv"

This will give you an output like this of all your 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: You will need to turn on the active directory windows feature to be able to use the activedirectory module. This can be found under 'Remote Sever Administration tools' tab in windows features.

Link: For Cmdlet's in the AD module

Soapsuds answered 8/8, 2013 at 10:56 Comment(1)
I can't believe that I didn't stumble across this, while searching the internet for a solution... Thank you very much.Eatage

© 2022 - 2024 — McMap. All rights reserved.