New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider adding an automatic variable for the last result of a computation #7853
Comments
$__ is too similar to $_ We showed how to do this, variable was $last, by wrapping Out-default in a proxy function. see page 382 of PowerShell in Action, third edition |
@Masterxilo: In addition to @RichardSiddaway's approach, # UPDATE: USE THE OTHER SOLUTION BELOW INSTEAD.
$PSDefaultParameterValues['*:OutVariable'] = '__' (Since this is a custom solution, I've stuck with your proposed name, Update: The following variant, courtesy of @vexx32, is preferable in general, and it also avoids the additional processing overhead that the solution above incurs, making the performance/resource-consumption debate below moot. A fundamental limitation, however, is the inability to capture output explicitly sent to $PSDefaultParameterValues['Out-Default:OutVariable'] = '__' For more information, see this answer I've just posted to the Stack Overflow question you link to above. |
@mklement0 Your suggestion doesn't save the output of the last command, it saves the output of every single command that gets run! It appears to work because only the last variable binding persists. Doing this means that If you have 3 commands in a pipeline then the output of each stage is saved in an |
To quote from my linked SO answer (update: since rewritten to recommend @vexx32's variant):
In practice, I would argue, this technique works fine in interactive sessions without noticeable performance impact in typical scenarios. |
@mklement0 I certainly run a lot of scripts from my interactive environment. In practice, this is a wasteful solution for something that can be achieved much more efficiently and with little effort. I don't recommend it in any scenarios. |
My solution is a pragmatic one that is trivial to implement. As for the wastefulness: My guess is that users won't notice the processing overhead in practice, while still reaping the benefits.
Do tell us how. If it's what @RichardSiddaway alluded to: it won't be nowhere near as simple. The above debate is moot, however, if we decide to make this a built-in feature, which sounds like a good idea - even if details need to be worked out (limit on in-memory size, ...). What do you suggest? |
Does Insert testing montage here... ... I might actually use this, it works pretty dang well. Easier than proxying the whole function, too! |
Excellent idea - that hadn't even occurred to me. Aside from incurring less processing overhead, there is a functional difference, however - still, that too could be considered an improvement;
|
I tend to use this way Get-Command *-PSRead* | Tee-Object -Variable ou which gives the output I want to the screen and to the though the disadvantage is that I need to know thats what I want to happen before issuing the command |
@mklement considering that it would be considered similar to In any case, I'm definitely adding that to my own profile, very handy! |
@vexx32: Agreed on all counts;
Yes, having output collected automatically has two advantages:
|
@vexx32: Unfortunately, while we can have our cake, we can only eat it partially:
An unsatisfying workaround is to capture In short: unless there is something I'm overlooking, it seems that writing an |
You know, that doesn't even really bother me that much. Most of the time, if I pipe to the Format-* cmdlets, I don't want to reuse that output -- if I did want it, I'd have to rewrite the line anyway to remove the format command. Once again... very little lost. :D |
@vexx32: Got it. |
From my understanding, Is there a worksaround? Or entirely impossible even by changing powershell itself? |
I'm sure it's possible. I'm not fully sure why calling an external executable bypasses |
@vexx32, I assume it's related to the by-design behavior of passing external programs' output streams through to the console (unless captured or redirected), enabling programs such as function Out-Default { Write-Host 'here' } # All PowerShell commands will now print just 'here'
whoami # external program: Out-Default is NOT called
# An explicit redirection routes the output through PowerShell's streams and
# therefore calls Out-Default,
# but *still passes the output through*(!) as well.
whoami *>&1 |
I should add that Currently, the simplest (but still cumbersome) workaround is: whoami | Write-Output # $__ is now populated. |
whoami | echo isn't bad at all! |
I don't know enough about the innards of PS memory management to judge how feasible this is, but: Just like bash and friends have |
Good idea, @elipsion. As an aside regarding the |
Thanks for sharing this. I was scratching my head why I couldn't get it to work when saving the output. I eventually used |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
2 similar comments
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes. |
Maybe calling it
$__
.See https://stackoverflow.com/questions/14351018/powershell-is-there-an-automatic-variable-for-the-last-execution-result
The text was updated successfully, but these errors were encountered: