August 30th, 2015

PowerTip: Retrieve Last Token with PowerShell

Doctor Scripto
Scripter

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 $$

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.