Skip to content

Select-PSObject from variable? #661

@FH-Inway

Description

@FH-Inway

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'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions