$PSCustomObject not displaying correctly

Welcome Forums General PowerShell Q&A $PSCustomObject not displaying correctly

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
6 months, 1 week ago.

  • Author
    Posts
  • #101748

    Participant
    Points: 0
    Rank: Member

    Greetings:

    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
    	[CmdletBinding()]
    	param
    	(
    		[parameter(Mandatory = $true,ValueFromPipeline =$true)]
    		[Alias("CN")]
    		[string] $computername
    	)
    	
    	$testconn = Test-Connection -ComputerName $computername -Quiet
    	if ($testconn)
    	{
    		Write-Host "Computer is online."
    	}
    	else
    	{
    		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."

    Thanks.

  • #101757

    Participant
    Points: 307
    Helping Hand
    Rank: Contributor

    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.

    • #101760

      Participant
      Points: 0
      Rank: Member

      Hello:

      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.

      -Brandon

The topic ‘$PSCustomObject not displaying correctly’ is closed to new replies.