theshizy
theshizy

Reputation: 545

Powershell - Change format of output

How can I change the presenation of the output my code produces:

$apps = Import-CSV apps.csv
$computers = Import-CSV compobj.csv
foreach($computer in $computers) {    
    $computerLob = $computer.lob
    $lobApps = $apps | ? {$_.lob -eq $computerLob }
    foreach($app in $lobApps){
       $computerHostname = $computer.hostname
       $appLocation = $app.location
       $installed=Test-Path "\\$computerHostname\$appLocation"      
       New-Object PSObject @{Computer=$computer.hostname;App=$app.appname;Installed=$installed} 
    }
}

I would like for the presentation of the code to be changed. This is how it looks like:

Name                         Value                                                                                                            
----                         -----                                                                                                            
Installed                    True                                                                                                             
App                          App1                                                                 
Computer                     171.159.192.10
Installed                    True                                                                                                             
App                          App2                                                                         
Computer                     171.159.192.10

I'd like for it to look like this:

Computer                 App1    App2
-----------              ------    -----
171.159.192.10           True     True

Upvotes: 1

Views: 381

Answers (3)

Keith Hill
Keith Hill

Reputation: 202012

If you are on PowerShell V3, rather than use new-object you can do this:

[pscustomobject]@{Computer=$computer.hostname;App=$app.appname;Installed=$installed} 

On V2, don't forget to use the -Property parameter e.g.:

new-object psobject -property @{Computer=$computer.hostname;App=$app.appname;Installed=$installed} 

And to force the output order you can use Format-Table:

$obj = new-object psobject -property @{Computer=$computer.hostname;App=$app.appname;Installed=$installed} 
$obj | Format-Table Computer,App,Installed

Upvotes: 1

Victor Zakharov
Victor Zakharov

Reputation: 26454

Here is what I mean (a follow-up to OP's question asked in comments, too big to fit there):

function MyFunction(){
  $apps = Import-CSV apps.csv
  $computers = Import-CSV compobj.csv
  foreach($computer in $computers) {    
    $computerLob = $computer.lob
    $lobApps = $apps | ? {$_.lob -eq $computerLob }
    foreach($app in $lobApps){
      $computerHostname = $computer.hostname
      $appLocation = $app.location
      $installed=Test-Path "\\$computerHostname\$appLocation"      
      New-Object PSObject @{Computer=$computer.hostname;App=$app.appname;Installed=$installed} 
    }
  }
}

MyFunction | select Computer,App,Installed

A reduced test case to prove the above should work:

function MyFunction(){
  New-Object PSObject -Property @{
    Computer="computer"
    App="app"
    Installed="installed"
  }
}

MyFunction | select Computer,App,Installed

Upvotes: 0

Joey
Joey

Reputation: 354784

You're passing the hashtable to New-Object as its ctor argument instead of a property set. Change it to:

New-Object PSObject -Property @{
  Computer=$computer.hostname
  App=$app.appname
  Installed=$installed
}

Upvotes: 2

Related Questions