$PSCustomObject not displaying correctly


This script is designed to look in the registry for installed applications on remote-PC’s. In its current state the script executes without issue, but the output is formatted in rows and I am looking for columns with rows. I am confident the issue is with the $PSCustomObject construct; when I remove the Invoke-Command the script formats output as intended. This is my first real try at using the $PSCustomObject accelerator type so hopefully I am not too far off…

function Get-InstalledApps
{#start of function
		[parameter(Mandatory = $true,ValueFromPipeline =$true)]
		[string] $computername
	$testconn = Test-Connection -ComputerName $computername -Quiet
	if ($testconn)
		Write-Host "Computer is online."
		Write-Host "Computer is offline."
	# Start of Invoke-Command scriptblock content
	$scriptblockcontent =
	$objs = @()
	$RegKey = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
	$InstalledAppsInfos = Get-ItemProperty -Path $RegKey
 Foreach($InstalledAppsInfo in $InstalledAppsInfos)
        $Obj = [PSCustomObject]@{Computer=$ComputerName;
                                 DisplayName = $InstalledAppsInfo.DisplayName;
                                 DisplayVersion = $InstalledAppsInfo.DisplayVersion;
                                 Publisher = $InstalledAppsInfo.Publisher}
        $objs += $Obj
		$objs | Where-Object { $_.DisplayName }
	} # End of scriptblock content
Invoke-Command -ComputerName $computername -ScriptBlock $scriptblockcontent
} #End of Function

As always, if there’s a more sensible (best practices) way to approach what I am trying to accomplish, I am “all ears.”


Using your code as is, why not just to this…

Get-InstalledApps -computername labpc01 | Format-Table -AutoSize

Computer is online.

Computer DisplayName                         DisplayVersion Publisher   PSComputerName RunspaceId               
-------- -----------                         -------------- ---------   -------------- ----------               
         Mozilla Firefox 59.0.2 (x64 en-US)   59.0.2         Mozilla    labpc01       9a04ae9d-5a92-4467-bf0...
         Mozilla Maintenance Service          59.0.2         Mozilla    labpc01       9a04ae9d-5a92-4467-bf0...

PoSH defaults to list view if you results exceed a specific column count. IF you want a table you have to reduce the count or force it using the format cmdlets.


What I ended up doing a while ago was adding the Format-Table to:

$objs | Where-Object { $_.DisplayName } | Format-Table

Absolutely worked; your suggestion and column count advisement was spot on. Thanks for your help.