PowerTip: Retrieve Last Token with PowerShell
Summary: Learn how to use Windows PowerShell to retrieve the last token and avoid retyping.
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?
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:\> $$
PS C:\> Stop-Process -Name $$