Author Posts

June 2, 2016 at 5:49 pm

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

June 2, 2016 at 6:05 pm

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.

June 2, 2016 at 7:02 pm

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;}

June 2, 2016 at 7:22 pm

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.

June 2, 2016 at 7:25 pm

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