1

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

3 Answers 3

2

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot. Having the same issue as above. I'm getting the columns in the following order Installed,App,Computer - can I have it in the following order: Computer,App,Installed.
@Antoine-LaurentLavoisier: Can't you pick any order with select statement in Powershell?
@Neolisk - can you show me a quick example please. Not sure how it works when you create an new object. I've been trying to switch the order in -Property @{...}
@Antoine-LaurentLavoisier: Whichever is not captured, is delivered onto a pipeline. Wrap your code into a function, call this function from your main code, then add a | select Computer,App,Installed to that. Does it make sense to you?
@Neolisk Unfortunately not :(. I'm fairly new to all this. Can you show me what you mean?
1

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

2 Comments

This works great! But I'm getting the columns in the following order Installed,App,Computer - can I have it in the following order: Computer,App,Installed. Tried playing around with the order of everything but it doesn't seem to work!
Interesting, I get the properties in that order on my system with PowerShell v4 now. You can always use Format-Table to select the specific order you want new-object ... | Format-Table Computer,App,Installed.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.