Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select-PSObject from variable? #661

Open
FH-Inway opened this issue Mar 8, 2025 · 3 comments · May be fixed by #664
Open

Select-PSObject from variable? #661

FH-Inway opened this issue Mar 8, 2025 · 3 comments · May be fixed by #664

Comments

@FH-Inway
Copy link

FH-Inway commented Mar 8, 2025

Select-PSFObject can add properties from objects other than the object it is selecting from like e.g. this:

$myObject = @{
  Id = "Test"
}

$myOtherObject = @{
  Name = "Test"
  Path = "C:\Temp\test.txt"
}

$myObject | Select-PSFObject 'Id as Name', 'Path from myOtherObject WHERE Name = Id'

I'm wondering if it is also possible to add a property based on a variable? I tried several variations of the following, but never got the expected result.

$myObject = @{
  Id = "Test"
}

$myVariable = "C:\Temp\test.txt"

$myObject | Select-PSFObject 'Id as Name', 'Path from myVariable'

Asked differently, is it possible to express the following two variations in a more succinct and readable way?

$myObject | Select-PSFObject 'Id as Name', @{Name = "Path"; Expression = { [string]$myVariable } }

# or

$myOtherObject = @{
  Path = $myVariable
}

$myObject | Select-PSFObject 'Id as Name', 'Path from myOtherObject'
@FriedrichWeinmann
Copy link
Member

It can't do that yet
But it definitely should be able to do that, so in the imminent future it will be able to :)

Thanks for the great suggestion!

FH-Inway added a commit to FH-Inway/psframework that referenced this issue Mar 17, 2025
@FH-Inway
Copy link
Author

More out of curiosity about how it works behind the scenes, I took a stab at this: FH-Inway@91b01c7

Needs obviously some work, but hopefully I'm on the right track here.

Used the following tests (as seemed to make more sense to me than from).

$myObject = @{
  Id = "Test"
}

$myVariable = "C:\Temp\test.txt"

$myObject | Select-PSFObject 'Id as Name', '$myVariable as Path'

<#
Name Path
---- ----
Test C:\Temp\test.txt
#>

Just the variable also works and uses the variable name as property name.

$myObject = @{
  Id = "Test"
}

$myVariable = "C:\Temp\test.txt"

$myObject | Select-PSFObject 'Id as Name', '$myVariable'

<#
Name myVariable       
---- ----------
Test C:\Temp\test.txt
#>

@FH-Inway FH-Inway linked a pull request Mar 22, 2025 that will close this issue
@FH-Inway
Copy link
Author

Poked a bit more and created #664 to enable a more targeted discussion of the changes.

Let me know if this is of use to you and feel free to ignore. I'm mainly working on this to satisfy my curiosity and learn something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants