What does ${$} mean / do in PowerShell?

7

While learning PS I executed 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 seems to retrieve last argument from previous command, but not quite sure how it works or what is actually doing.

I will appreciate an explanation or link to doc/explanation.

Ps 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

Allende

Posted 2020-02-25T21:48:17.637

Reputation: 243

Answers

8

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

conditioHumana

Posted 2020-02-25T21:48:17.637

Reputation: 96

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 – 2020-02-25T22:30:54.617