Showing results using out-gridview

Darren Rose 311 Reputation points
2022-11-24T21:56:25.557+00:00

The results of my script are stored in $results and I can see the results clearly by just typing

$results  

or

$results | out-gridview  

263969-image.png

But I want to also show the name of the computer the results came from

If I use

$results | Select-Object -Property PSComputerName | Out-GridView   

Then it just shows the computer name and not the results

264062-image.png

How can I show both columns of information in the grid please?

The contents of $results comes from the following code being run

$regpath = 'registry::HKEY_USERS'  
  
$results = @()  
  
$HKUserList = Invoke-Command -ComputerName $computers { param($regpath) Get-ChildItem $regpath | Select-Object -ExpandProperty Name } -ArgumentList $regpath  
  
ForEach ($HKUser in $HKUserList) {  
  
    $regpath2 = 'registry::' + $HKUser + '\Software\Microsoft\Office\16.0\Outlook\Search'  
  
    $PSTFind = Invoke-Command -ComputerName $computers { param($regpath2) Get-ChildItem -path $regpath2 -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Property | Where-Object -FilterScript {$_ -like "*.pst"} } -ArgumentList $regpath2  
      
    ForEach ($PST in $PSTFind) {  
  
    If (Test-Path -Path $PST -PathType leaf)  
        {  
            Write-Host "PST found at '$PST'"  
            $results += $PST  
        }  
  
   }     
  
}  
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-11-24T22:28:00.533+00:00

    Assuming your data is a series of Objects of some sort (you don't offer any information on the object type of the data items) that has properties named "PSComputeName" and "String", just modify the Select-Object:

    $results | Select-Object -Property PSComputerName,String | Out-GridView  
    

  2. Rich Matheisen 47,901 Reputation points
    2022-11-25T04:09:05.25+00:00

    I only have a single machine that has MS Office installed on it, so the code below lacks the "-ComputerName" parameter. BTW, a single user may have multiple PST files. I'm not sure your code handles them in a way that would be usable if you decided to send the values to a CSV because a CSV doesn't understand how to handle an array (a GridView does, though).

    Invoke-Command  -ScriptBlock{  
        (Get-ChildItem registry::HKEY_USERS).name |  
            ForEach-Object{  
                $regpath2 = 'registry::' + $_ + '\Software\Microsoft\Office\16.0\Outlook\Search'  
                [array]$results = @()  
                Get-ChildItem -path $regpath2 -ErrorAction SilentlyContinue |   
                    Select-Object -ExpandProperty Property |  
                        Where-Object -FilterScript {$_ -like "*.pst"} |  
                            ForEach-Object{  
                                If (Test-Path -Path $_ -PathType leaf){  
                                    $results += $_  
                                }  
                            }  
                if ($results.count -gt 0){  
                    [PSCustomObject]@{  
                        Computer = $ENV:ComputerName  
                        UserSID = $_  
                        PATH = $results -join ';'  
                    }  
                }  
            }  
        }  
    

  3. Rich Matheisen 47,901 Reputation points
    2022-11-26T03:36:18.417+00:00

    I think the easiest way to work around this without passing user names and passwords would be to do the test for the presence of the PST after the Invoke-Command has completed.

    $computers = @("PC-1", "PC-2")  
          
    $results = Invoke-Command -ComputerName $computers -ScriptBlock {  
        (Get-ChildItem registry::HKEY_USERS).name |  
            ForEach-Object {  
                $regpath2 = 'registry::' + $_ + '\Software\Microsoft\Office\16.0\Outlook\Search'  
                [array]$psts = @()  
                Get-ChildItem -Path $regpath2 -ErrorAction SilentlyContinue |   
                    Select-Object -ExpandProperty Property |  
                        Where-Object -FilterScript { $_ -like "*.pst" } |  
                            ForEach-Object {  
                                $psts += $_  
                            }  
                        }  
            [PSCustomObject]@{  
                Path = $psts  
            }  
    }  
      
    $results |  
        ForEach-Object{  
            [PSCustomObject]@{  
                PSComputerName = $_.PSComputerName  
                PSTs = [array]@()  
            }  
            $_.Path |  
                ForEach-Object{  
                    If (Test-Path -LiteralPath $_ -PathType leaf) {  
                        $OnlyPSTs.PSTs += $_  
                    }  
                }  
        } | Out-GridView  
    

    Your code still hadn't taken into account that there may be more than one PST file per user. The code above does that.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.