In PS, when using Select-Object
, the -Property
param "specifies the properties to select":
The value of the Property parameter can be a new calculated property. To create a calculated, property, use a hash table.
Valid keys are:
- Name:
<string>
- Expression:
<string>
or<script block>
As an starting point, here's an anonymous object
$person = [pscustomobject]@{
FirstName = "Kyle";
LastName = "Mit"
}
The following calculated property works just fine:
$person | Select-Object 'FirstName', 'LastName',
@{
Name="FullName";
Expression= { "$($_.FirstName) $($_.LastName)" }
}
Which correctly returns the following:
FirstName LastName FullName
--------- -------- --------
Kyle Mit Kyle Mit
However, I'm trying to build a calculated property that takes some complicated logic and requires multiple lines to implement. As soon as I have another return value inside the expression both values are returned.
So the following query will return FullName
as { false, "Kyle Mit" }
$person | Select-Object 'FirstName', 'LastName',
@{
Name="FullName";
Expression= {
"test" -eq "example"
return "$($_.FirstName) $($_.LastName)"
}
}
Which returns the following:
FirstName LastName FullName
--------- -------- --------
Kyle Mit {False, Kyle Mit}
But I'm attempting to explicitly return just the last statement. Delimiting with ;
or using return
doesn't seem to help.
Any ideas?