Objects returned as properties with Export-CSV

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Mark Hammonds Mark Hammonds 1 year, 5 months ago.

  • Author
    Posts
  • #32690
    Profile photo of Bill Frazier
    Bill Frazier
    Participant

    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

  • #32692
    Profile photo of Mark Hammonds
    Mark Hammonds
    Participant

    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         
    
  • #32693
    Profile photo of tommymaynard
    tommymaynard
    Member

    Are you seeing System.Object[] in your Csv file? If so, start at Boe Prox's post about this here: http://learn-powershell.net/2014/01/24/avoiding-system-object-or-similar-output-when-using-export-csv. I've reference this post a few times, and have incorporated it in at least two scripts/functions.

  • #32694
    Profile photo of Bill Frazier
    Bill Frazier
    Participant

    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!

  • #32695
    Profile photo of Mark Hammonds
    Mark Hammonds
    Participant

    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 😀 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

  • #32696
    Profile photo of Bill Frazier
    Bill Frazier
    Participant

    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.

  • #32698
    Profile photo of Mark Hammonds
    Mark Hammonds
    Participant

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

You must be logged in to reply to this topic.