export csv outputs object type, not value

Welcome Forums General PowerShell Q&A export csv outputs object type, not value

This topic contains 4 replies, has 3 voices, and was last updated by

ca
 
Participant
2 years, 5 months ago.

  • Author
    Posts
  • #41482
    ca

    Participant
    Points: 0
    Rank: Member

    When I run the script below, instead of the values of each object being output to a csv, they are output like the following:

    IsReadOnly IsFixedSize IsSynchronized Keys Values SyncRoot Count
    FALSE FALSE FALSE System.Collections.Hashtable+KeyCollection System.Collections.Hashtable+ValueCollection System.Object 3
    FALSE FALSE FALSE System.Collections.Hashtable+KeyCollection System.Collections.Hashtable+ValueCollection System.Object 3
    ........................ect

    Anyone know what is going on?

    This is the script:

    $90 = (get-date).adddays(-90);
    $path = (Get-ChildItem "C:\Program Files" -Recurse ) | % { $_.FullName }
    $hash = foreach ($p in $path) {
    $val = Get-Item -Path $p
    if ($val.LastWriteTime -lt $90) {
    	[pscustomobject]@{
    	Item=$val.FullName
    	lastAccess=$val.LastAccessTime 
    	lastWrite=$val.LastWriteTime }}}
    $hash | Export-Csv C:\scripts\accessed.csv -NoTypeInformation
  • #41488

    Keymaster
    Points: 1,619
    Helping HandTeam Member
    Rank: Community Hero

    Wow, that's a convoluted little chunk of code ;). What are you getting if you pipe $hash to Get-Member before exporting it? It's possible the way you're loading $hash is making it so Export-Csv isn't treating it correctly, but can't be sure.

  • #41514
    ca

    Participant
    Points: 0
    Rank: Member

    PS C:\scripts> $hash | gm

    TypeName: System.Collections.Hashtable

    Name MemberType Definition
    —- ———- ———-
    Add Method System.Void Add(System.Object key, System.Object value)
    Clear Method System.Void Clear()
    Clone Method System.Object Clone()
    Contains Method bool Contains(System.Object key)
    ContainsKey Method bool ContainsKey(System.Object key)
    ContainsValue Method bool ContainsValue(System.Object value)
    CopyTo Method System.Void CopyTo(array array, int arrayIndex)
    Equals Method bool Equals(System.Object obj)
    GetEnumerator Method System.Collections.IDictionaryEnumerator GetEnumerator()
    GetHashCode Method int GetHashCode()
    GetObjectData Method System.Void GetObjectData(System.Runtime.Serialization.SerializationInfo inf...
    GetType Method type GetType()
    OnDeserialization Method System.Void OnDeserialization(System.Object sender)
    Remove Method System.Void Remove(System.Object key)
    ToString Method string ToString()
    Item ParameterizedProperty System.Object Item(System.Object key) {get;set;}
    Count Property System.Int32 Count {get;}
    IsFixedSize Property System.Boolean IsFixedSize {get;}
    IsReadOnly Property System.Boolean IsReadOnly {get;}
    IsSynchronized Property System.Boolean IsSynchronized {get;}
    Keys Property System.Collections.ICollection Keys {get;}
    SyncRoot Property System.Object SyncRoot {get;}
    Values Property System.Collections.ICollection Values {get;}

  • #41527

    Member
    Points: 0
    Rank: Member

    Are you running this on PowerShell v2? That's the only way I can see you'd wind up with a hashtable object instead of a PSObject there.

    The [pscustomobject] accelerator requires PSv3 or later. In v2, use the New-Object cmdlet instead.

  • #41529
    ca

    Participant
    Points: 0
    Rank: Member

    Dave, yes, I was using version 2 : ( I did finally use the New-Object cmdlet. Thanks both for the help!

The topic ‘export csv outputs object type, not value’ is closed to new replies.