8

While learning PowerShell, I executed the command ${$} by mistake and got an output like this:

PS C:\Users\myuser> Get-ChildItem Env: | Out-File -FilePath $home\env.txt
PS C:\Users\myuser> ${$}
$home\env.txt

First I believed it was something like $_, but doing ${$_} does not behave the same.

As per other samples I tried, it seems to retrieve the last argument from the previous command, but I'm not quite sure how it works or what it is actually doing.

I would appreciate an explanation or link to documentation/explanation.

PowerShell host information, if needed:

Name : ConsoleHost
Version : 5.1.17134.858
InstanceId : [Removed as I don't if can be shared careless]
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : en-US
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled : True
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace

9

The input ${$} gets evaluated as the automatic variable $$. $$ itself is set to the last token of the last input line in the session. In your case it is set to the last argument home\env.txt

You can see this effect also with input like ${?} that results in $?, another automatic variable, which contains the result of the last executed command.

For a list of automatic variables see: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables

| improve this answer | |
  • Awesome ! Haven't read any until know for this "automatic variables", thank you. Now I understand why first google attempt didn't return something useful – Allende Feb 25 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.