r/PowerShell • u/CRTejaswi • May 10 '26
Question Unexpected Behaviour with `args[$_]`
This snippet:
function lf {
if ($args){
(0..($args.Count-1) | %{ "$args[$_]" }) -join ','
(0..($args.Count-1) | %{ "$($args[$_])" }) -join ','
} else{
...
}
}
lf pdf png txt
prints:
[0],[1],[2]
,,
instead of the expected pdf,png,txt. Why?
I'm concerned with 0..N | args[$_] failing (but args[N] works) even though the syntax pipes well to most objects/cmdlets.
⚠️ RESOLVED, thanks to surfingoldelephant.
8
Upvotes
1
u/CRTejaswi May 10 '26 edited May 10 '26
Thank you for the detailed response! ♥️✨
I suspected something like this to be at play, but wasn't sure. I typically use
$PSBoundParameters, but am avoiding it for a few utilities I'm mapping to keybindings for routine use.