user1443098
user1443098

Reputation: 7645

PowerShell Format-Table want properties as column headers

With this code:

$l | ForEach-Object -Process {
    $p = ($_.Folder + '\' + $_.Filename).Replace("{yymmdd}", "190911")
    if (Test-Path $p) {
        Write-Output @{Portfolio = $_.Portfolio; Path = $p; CreateTime = (Get-ChildItem $p).CreationTime}
    } else {
        Write-Output @{Portfolio = $_.Portfolio; Path = $p; CreateTime = "not found"}
    }
} | Format-Table

I get:

Name                           Value
----                           -----
Path                           S:\Data\190911\LN190911.txt
Portfolio                      CALoan
CreateTime                     9/12/2019 6:29:47 AM
Path                           S:\Data\190911\SL190911.txt
Portfolio                      CALoan
CreateTime                     9/12/2019 6:29:46 AM

I want the Names to be column headers and the Values to appear under each column. How do I get that done?

Upvotes: 2

Views: 1928

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36297

This is simply a matter of converting your hashtables to objects. Remove the Write-Output, and add the type [pscustomobject] before your hashtable and it will do what you want.

$l | ForEach-Object -Process {
    $p = ($_.Folder + '\' + $_.Filename).Replace("{yymmdd}", "190911")
    if (Test-Path $p) {
        [PSCustomObject]@{Portfolio = $_.Portfolio; Path = $p; CreateTime = (Get-ChildItem $p).CreationTime}
    } else {
        [PSCustomObject]@{Portfolio = $_.Portfolio; Path = $p; CreateTime = "not found"}
    }
} | Format-Table

Upvotes: 4

Related Questions