1

How can I past only a selection of fields from an object to show in out-gridview, but still keep the whole object as a result.

For example, I retrieve an object that gives me:

$listcreds = get-listofcredentials

Id                    : 03c0e0c0-0951-4698-9ba9-a70508b5467f
IsLocalProtect        : True
Name                  : vsphere.local\Administrator
CurrentUser           : False
UserName              : vsphere.local\Administrator
UserNameAtNotation    : Administrator@vsphere.local
UserNameSlashNotation : vsphere.local\Administrator
UserNameOnly          : Administrator
DomainName            : vsphere.local
EncryptedPassword     : Veeam.Backup.Common.CCodedPassword

In the gridview I want to only see Name and ID. After the user selects the row desired, I would like to have the result as the same type of object again.

When I use select-object,

$selectedcred = $listofcredentials | select-object Name, Id | out-gridview -passthru

I get the result back but I would now have to search the original object again to get the other properties of that row.

What better way to do this?

Gabrie
  • 537
  • 1
  • 5
  • 15
  • 1
    Sometimes there no easy shortcut. Sometimes you have to go long way. ;-) – Olaf Sep 23 '21 at 10:15
  • 1
    Normally, `Out-GridView` does not return any objects. When using the `PassThru` parameter, the objects representing the **selected rows** are returned to the pipeline. – JosefZ Sep 23 '21 at 12:46
  • I understand, but would like to only show certain columns of the object presented to out-gridview – Gabrie Sep 23 '21 at 13:01
  • 1
    `$cred = $listofcredentials | Where-Object { $_.Id -eq $selectedcred.Id }` – Theo Sep 23 '21 at 14:59
  • Yes, that is how I do it now and where I have the luck that ID indeed is unique, but this isn't always the case. But I learned from the other replies, there is no other way. – Gabrie Sep 24 '21 at 06:12

1 Answers1

1

You need to find the full object again in the list with Where-Object

This answer assumes the Id property is unique for every item in $listcreds

$listcreds = get-listofcredentials
$selectedcred = $listofcredentials | select-object Name, Id | out-gridview -passthru
$selectedcred = $listcreds | Where-Object {$_.Id -eq $selectedcred.Id}

I don't think there's a better solution here. If performance is a concern, you can convert the Where-Object into a foreach as below:

$listcreds = get-listofcredentials
$selectedcred = $listofcredentials | select-object Name, Id | out-gridview -passthru

foreach ($cred in $listcreds) {
    if ($cred.Id -eq $selectedcred.Id) {
        $selectedcred = $cred
        break
    }
}

However, the performance difference may be negligible or even negative.

spicy.dll
  • 948
  • 8
  • 23
  • 1
    OP did already state this in their post :) "I get the result back but I would now have to search the original object again to get the other properties of that row." – Daniel Sep 23 '21 at 16:53
  • 1
    @Daniel I didn't see that, whoops. However, I don't think there's a better way to do this. – spicy.dll Sep 23 '21 at 17:00
  • Thank you for all the replies. Too bad there is no other way. The example using ID is indeed easy since the ID is unique, but I don't always have the option to make it completely unique without exposing some fields that I'd rather not show. But that is just the way it is :-) – Gabrie Sep 24 '21 at 06:11