Home > OS >  Format output in Powershell
Format output in Powershell

Time:04-23

I have a small code in my script that is working well. I'm just annoyed with the output..

My output looks like this:

11.11.111.123

Model                                                                                                                                                                                  
-----                                                                                                                                                                                  
HP ZBook Studio G5                                                                                                                               

csname         : XXXXXXX
LastBootUpTime : 22/Apr/2022 08:10:57

But I want it like this:

IP Address:     11.11.111.123
Model:          HP ZBook Studio G5     
csname:         xxxxx
LastBootUpTime: 22/Apr/2022 08:10:57

This is the script:

Get-WmiObject Win32_NetworkAdapterConfiguration -Computername $pcName |
      Where { $_.IPAddress } |
      Select -Expand IPAddress | 
      Where { $_ -like '10.11*' -or $_ -like '10.12*'}
   
Get-WmiObject -Class Win32_ComputerSystem -Computername $pcName | Select Model
   
Get-WmiObject win32_operatingsystem -Computername $pcName -ea stop | select csname, @{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}} | format-list

CodePudding user response:

Since the output is produced by 3 different classes the way around it is create a new object to merge them:

$IPs = Get-CimInstance Win32_NetworkAdapterConfiguration -ComputerName $pcName |
    Where-Object { $_.IPAddress -like '10.11*' -or $_.IPAddress -like '10.12*' }
$Model = (Get-CimInstance -Class Win32_ComputerSystem -ComputerName $pcName).Model
$OS = Get-CimInstance win32_operatingsystem -EA Stop -ComputerName $pcName

[pscustomobject]@{
    'IP Address'   = $IPs.IpAddress -join ', '
    Model          = $Model 
    csname         = $OS.CSName
    LastBootUpTime = $OS.LastBootUpTime.ToString()
}
  • Related