PowerTip: Retrieve Last Token with PowerShell


Summary: Learn how to use Windows PowerShell to retrieve the last token and avoid retyping.

Hey, Scripting Guy! Question I used Get-Process to view a process I launched, and later I wanted to stop the process. How can I do this without
           doing a lot of typing or creating new variables?

Hey, Scripting Guy! Answer The $$ automatic variable contains the last token submitted, so do the following:

PS C:\> notepad

PS C:\> gps $$

Handles  NPM(K)    PM(K)    WS(K) VM(M)   CPU(s)   Id ProcessName

——-     ——    —–      —–   —–   ——        —         ———–

    116       9     1728       7396 …67     0.06     4668             notepad

At this point, Notepad is still the last token, so you can view it directly, and use it to stop the process:

PS C:\> $$

notepad

PS C:\> Stop-Process -Name $$

Comments (0)

Skip to main content