The Great Dollar Dollar

PowerShell Team

No, I’m not talking about our newest rap single. I’m talking about PowerShell’s most hidden gem. The one you can show to most members of even the PowerShell team, and have them gasp – “oh, that’s cool! I gotta remember that!”

And it’s not even for lack of documentation! Here are the first few lines of Get-Help about_automatic_variables:

PS>Get-Help about_automatic_variables

    Describes variables that store state information for Windows PowerShell.
    These variables are created and maintained by Windows PowerShell.

    Here is a list of the automatic variables in Windows PowerShell:

       Contains the last token in the last line received by the session.


That’s it, right at the beginning

The ‘$$’ (“dollar dollar”) automatic variable always holds the last thing on the last line you typed. Now, why would you ever care about tokens received by the session? It saves you typing, that’s why!

Imagine you’re parched and nearly exhausted from typing in some long path name. And to make it worse, just to see if a file exists:

PS>dir C:\windows\system32\drivers\etc\hosts

    Directory: C:\windows\system32\drivers\etc

Mode                LastWriteTime     Length Name
—-                ————-     —— —-
-a—         11/7/2010   1:39 PM        848 hosts

Now that you’ve found it, you want to open it in Notepad. While you can normally press the up arrow, go back to the beginning of the line and change ‘dir’ to ‘Notepad’, there’s an easier way:

PS>notepad $$

Voila – notepad opens ‘c:\windows\system32\drivers\etc\hosts’.

If you’re a shell polyglot, the Bash shell (in Unix, Mac, etc.) supports something similar through its ‘!$word designator.


Lee Holmes [MSFT]
Windows PowerShell Development
Microsoft Corporation


Discussion is closed.

Feedback usabilla icon