r/PowerShell • u/Discuzting • Apr 01 '26
Fellow interactive users, what's the most "ergonomic" way to extract properties?
I suppose this is a bit of an odd problem and is more about sharing tips and tricks, this is NOT a XY problem!
What I come across a lot are situations where I have to interactively (not through scripts) access properties of evaluation results.
For example let's say I copied a JSON to my clipboard and I want data.b's value:
{
"data": {
"a": 1,
"b": 2
}
}Naturally we could just do Get-Clipboard and ConvertFrom-Json in a pipeline, and I have an alias cfj for ConvertFrom-Json, so this is just gcb|cfj.
Now the goal is to obtain the property and output it on the success stream. What is the fastest to type or the most ergonomic way to do this while remaining in the mental flow?
I currently have four general solutions depending on how I'm feeling:
1. The most obvious solution: wrap the entire expression in brackets and perform property access.
(gcb|cfj).data.bThis is succinct and very readable, but sometimes if you are doing the pipeline style it feels bad to wrap the entire pipeline back into an expression.
Practically this is to 1. press "Home" key, 2. type "(", 3. press "End" key, type ")". This sequence of input is fine but sometimes I just don't enjoy doing this.
2. Use the standard command Select-Object -ExpandProperty
I have an alias sco for Select-Object so this is just:gcb|cfj|sco -exp data|sco -exp b
Any sane person would see how stupid this is, but hey, it feels great to keep piping forward!
This is fine if the property is just one level deep, but with properties multiple levels deep there is just too much to type. Not very readable neither.
3. Store in a temporary variable then perform property access
$t=gcb|cfj;$t.data.b
This one is also pretty obvious, the main advantage for this is how we could avoid typing brackets and we also have a "checkpoint" temporary variable.
Could also use Tee-Object as well but that is really verbose, plus the contents are outputted to success stream unless we pipe it away or something.
gcb|cfj|tee -va t;$t.data.b
4. Custom transformation functions
Coming from the Kotlin programming language, I have defined a utility function `let` in my PowerShell profile which allows me to do this:
gcb|cfj|let{$_.data.b}
New-Alias -Name let -Value Invoke-AllValues
function Invoke-AllValues {
<#
.SYNOPSIS
Invoke script block once with all the values from pipeline.
.PARAMETER InputObject
Designed to be used through pipeline.
Scalar argument provided through parameter will be wrapped into an array.
.PARAMETER Block
Input is passed to the ScriptBlock as `PSVariable` named `$_`, which is always an array.
.EXAMPLE
'a','b','c' | Invoke-AllValues { $_.Count } | ForEach-Object { "Recceived $_ objects" }
Received 3 objects
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)][AllowNull()]
[Object]$InputObject,
[Parameter(Mandatory, Position = 0)][ArgumentCompletions('{}')]
[ScriptBlock]$Block
)
# Replace `$InputObject` with pipeline value, if value is provided from pipeline.
if ($MyInvocation.ExpectingInput) { $InputObject = $input }
else { $InputObject = @($InputObject) }
$Block.InvokeWithContext($null, [PSVariable]::new('_', $InputObject))
}It is clearly not the idiomatic way to do things here, but if it works it works. 🤷
Thoughts on better ways to deal with the problem? I appreciate all comments, thanks for reading!
6
u/justaguyonthebus Apr 01 '26
Honestly, I'm assigning it to a variable on the first line to do the transformation once. Then exploring the variable on its own line. I'm using new line over semicolon unless I actually need the commands to always run together.
Here is a pipeline hack I use instead of select object expand property that you will appreciate:
6
u/surfingoldelephant Apr 01 '26 edited Apr 01 '26
Use a
PSReadLinecustom key handler. I have one that inserts matching(,{,[,<pairs. Here's the code for it.So for example, selecting all text (
Ctrl+Aon Windows) and typing(will wrap the text in(...)and move the cursor to the end. If there's no text selected, it'll insert a pair of()and move the cursor inside. PressingAlt+n(like5) beforehand will insertnnumber of pairs.Â
Here's a different approach:
Now instead of
gcb|cfj|let{$_.data.b}, your call becomes:Note the
,(not a.) as it's passing an array of member names. If you preferdata.b, change$Memberto[string]and split on.before performing member-access.Another option is to use
ValueFromRemainingArguments:Which would allow you to pass member names like this:
However, you lose pipeline input, as the second member argument will bind to
$InputObject(due to the[Object]type) before the actual pipeline input gets a chance to bind.3
u/JeremyLC Apr 01 '26
I would choose how to extract properties based on the context in which I need them. Each of your examples has an appropriate use case.
I also never worry about how many keystrokes I'm typing. My tab key works and I can't remember all the aliases anyway.
(I also don't combine lines with semicolons all over the place. It really doesn't make sense in an interactive session, and scripts should be written for clarity, not size. It doesn't matter how many total lines you have, or how many of those are single characters, it matters whether you can look at your code a week from now and still figure out what it does. /rant :)
2
u/realslacker Apr 01 '26
  gcb|cfj|%{$_.data.b}
2
u/PinchesTheCrab Apr 01 '26
Totally unrelated to the problem at hand, but don't forget about [alias()] exists. You can keep it inside your functions instead of relying on the user executing a separate new-alias command.
On this issue though, I don't understand the two examples:
In this case I think these two approaches are pretty much equivalent:
For this one I feel like just using
foreach-objectwould have the same effect?8
u/ostekages Apr 01 '26 edited Apr 01 '26
I would forget that I had created all those aliases and continue using the full command name 😂
Is ergonomic really == fastest? Fastest for me, is what I remember. Ergonomic, then yes you can argue typing less is better.
1
u/da_chicken Apr 01 '26 edited Apr 01 '26
The friction that you're going to have is that Powershell prefers to output tables, and JSON tends to be heirarchical.
In general, the idiomatic way to display object properties is
Format-Table -AutoSize(ft -a) orFormat-List -Properties *(fl *).It's basically the same impedence mismatching that you get with mapping JSON or XML to a relational database.
The "intended" way to accomplish this idea is the format.xmlps1 system, but in practice nobody really does that unless they're authoring a module. It basically requires objects to have their own .Net class (I'm not sure if it works with Powershell classes) and that won't happen when shredding arbitrary serialized data. Either way, though, it's also not really an improvement, per se, as
Get-ChildItem -Recursein a folder with child folders is an example of how the system outputs. Folders display different than files, but it's not entirely ideal for displaying.Personally, I would write a function like this (this one is AI generated, and I'm not someplace I can test it, but it reads right to me):
It shouldn't be that difficult to take that function and make it display output in a way that makes sense to you. You could pretty easily make it display similarly to the Linux
treecommand.Notes:
The
PSObjectproperty is an intrinsic property on most objects in Powershell. It's intended for reflection and introspection.Get-Membercan also help with that, but this is a bit easier to work with sometimes.You'll also notice that this above function does the idiomatic way to accept pipeline input. There's more about that in the advanced functions doc. I would not do it the way you have.
Finally, I would agree with some others here that ergonomic is a weird optimization criteria. Tab completion is a thing, and the mental load of remembering every alias is probably not as free as it feels.
3
u/purplemonkeymad Apr 01 '26
I use
all the time interactively. If the prior command has properly implemented output type, you even get autocomplete.
2
u/Over_Dingo Apr 01 '26 edited Apr 01 '26
Personally I often use
ForEach-Object -MemberNamewhich is the first positional parameter so it can be used for example:ls | % gettypeand can also call methods, otherwise I don't see many differences between select -expand2
u/OPconfused Apr 01 '26
I created a function to access nested properties for this reason of convenience. It supports wildcards. Your example would be
cfj | select-property data.borcfj | slp *.b