What does ${$} mean / do in PowerShell

powershellpowershell-5.0

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

Best Answer

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

Related Question