export csv outputs object type, not value

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of ca ca 3 months, 4 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #41482
    Profile photo of ca
    ca
    Participant

    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
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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
    Profile photo of ca
    ca
    Participant

    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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    Profile photo of ca
    ca
    Participant

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

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.