Using piping or other sort of inline notation, I'd like to "select" certain values in a hash table straight through to the result, while changing or setting others.
Hash Table
I'm starting with a hash table (which, frankly is being used like an object), like the following:
>$hash
Name Value
---- -----
System server-1
Job Microsoft.PowerShell.Commands.PSWmiJob
Result
Is there a way to use the Select-Object cmdlet (or anything like it) to set the value of certain properties and pass through others? I'd like to end up with this:
>$hash
Name Value
---- -----
System server-1
Job Microsoft.PowerShell.Commands.PSWmiJob
Result Good
If I didn't need/want it to be inline, I simply set the value of Result
:
$hash.Result = "good"
For an inline solution, I could always use Foreach-Object
like this:
$hash | Foreach-Object {@{System=$_.System;Job=$_.Job;Result="Good"}}
or just directly refer to $hash in a sub-expression:
${@{System=$hash.System;Job=$hash.Job;Result="Good"}}
However, it seems nonsensical to use Foreach-Object
when I'm only processing the one hash table, and quite verbose to mention properties twice that I just want to pass through anyways, and then can be unwieldy if my $hash
variable is $aSignificantlyLongerHashTableVariableName
.
If I was using a Powershell Object...
If I were using an object like this:
>$obj
System Job Result
------ --- ---
server-1 Microsoft.PowerShell.Commands.PSWmiJob
I would use the select-object
Cmdlet like this:
>$obj | Select-Object System, Job, @{Name="Result";Expression="Good"}
which would result in
>$obj
System Job Result
------ --- ---
server-1 Microsoft.PowerShell.Commands.PSWmiJob Good
Hashtable and PSObject have some similar aspects (something that contains other things in a named fashion), but AFAIK, they're incompatible - Is there an alternative closer to Select-Object
for what I'm trying to do here?