Objects returned as properties with Export-CSV

I’ve got the following code to export local account info. When I send my $account collection to export-csv my Username and LastLogin properties get populated with Object information and not the properties I want. Outputting $account to the screen shows the info but it’s bracketed, and I believe that means it’s a collection. When I pipe $user.name to GM it tells me it’s a system.string object but export csv says it’s a collection. (System.DirectoryServices.PropertyValueCollection) I’m missing something here and I’m needing some direction/hints on where to look.

$ADSI = [ADSI]"WinNT://$env:COMPUTERNAME"
$Users = $ADSI.Children | where {$_.SchemaClassName -eq 'user'}
$BadUser = "User has never logged in."

$Accounts = @()
ForEach ($User in $Users) {

    If ($User.LastLogin) {
        $Login = $User.Lastlogin
        } Else {$Login = $BadUser}

    $Properties = [ordered]@{
                UserName=$User.name
                LastLogin=$Login
                'PWD Age'="{0:N0}" -f ($User.PasswordAge.value/86400)}

    $Accounts += New-Object -TypeName psobject -Property $Properties
}

UserName LastLogin PWD Age
{User1} {12/7/2015 7:53:24 AM} 3
{User2} {12/3/2015 11:32:47 AM} 49
{User3} User has never logged in. 0
{User4} {12/7/2015 7:46:01 AM} 39
{User5} User has never logged in. 0

You already have them in a custom object called $properties

Tested This does Work

change to this

$ADSI = [ADSI]"WinNT://$env:COMPUTERNAME"
$Users = $ADSI.Children | where {$_.SchemaClassName -eq 'user'}
$BadUser = "User has never logged in."

$Accounts = @()
ForEach ($User in $Users) {

    If ($User.LastLogin) {
        $Login = $User.Lastlogin
        } Else {$Login = $BadUser}


$Accounts += New-Object PSObject -Property([ordered]@{UserName=$($User.name);LastLogin=$($Login);'PWD Age' = $("{0:N0}" -f ($User.PasswordAge.value/86400))})
}

$Accounts | Export-Csv "c:\temp\accounts.csv" -NoTypeInformation

Output

UserName                     LastLogin                        PWD Age   
--------                     ---------                        -------   
camadmin                     8/4/2014 11:36:56 AM             0         
camguest                                                      0         

Are you seeing System.Object in your Csv file? If so, start at Boe Prox’s post about this here: Avoiding System.Object[] (or Similar Output) when using Export-Csv | Learn Powershell | Achieve More. I’ve reference this post a few times, and have incorporated it in at least two scripts/functions.

Mark Hammonds,
BINGO! We have a winner, that fixed it. Now I need to know WHY it fixed it so I have a better understanding of what just happened. Any guidance?

THANK YOU!

Well I put the three fields into one custom object and expanded the property by enclosing it with $() like so $($User.name) that means insert the value not the object. I think :smiley: you will run into the a lot when trying to add a variable with a property to a sting like so

Out-host “The users user ID is $($User.name)”

i’m not the best person to explain these things as I have only been doing power shell for about 12 weeks

Mark,
As soon as I hit send on my reply I had my epiphany and saw what I did wrong. I had run into this issue back in March of this year and completely forgot what I had to do to correct it. What do you want to bet I’m closer to burning this into my brain now? THANK YOU for the time and knowledge.

heh you guys have helped me many times in the past i’m just glad I can contribute in return