user1005246
user1005246

Reputation: 135

powershell get all OS files in csv with version

I am trying to run a powershell script that walks through all files in a system and creates a text file with one files per row. I need name, directory, size, version and last modified. This code below works fine in Version 5 (it will throw errors if access is denied)... but not in version 2....

Get-ChildItem -Path . -Recurse |
    foreach-object {
        [pscustomobject]@{
            Name = $_.fullname;
            DateModified = $_.LastWriteTime;
            Version = $_.VersionInfo.FileVersion;
            Length = $_.length;
        }
    } | Export-Csv -Path "c:\workingfiles\post.csv" -NoTypeInformation

Where every line looks like:

"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","4"

how can I get what I need in version 2? (not possible to upgrade)

-Ken

Upvotes: 0

Views: 226

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174505

Instead of casting the hashtable to pscustomobject, call New-Object -Property:

Get-ChildItem -Path . -Recurse |ForEach-Object {
    New-Object psobject -Property @{
        Name = $_.FullName;
        DateModified = $_.LastWriteTime;
        Version = $_.VersionInfo.FileVersion;
        Length = $_.Length;
    }
} | Export-Csv -Path "c:\workingfiles\post.csv" -NoTypeInformation

Upvotes: 1

Related Questions