Short script to open notepad.exe (can be any process you want to open) on either your local machine or a remote machine and then kill the process.
Option 1

C:\PSTools\PsExec.exe cmd /c start notepad.exe
Start-Sleep -s 10
C:\PSTools\PsKill.exe -accepteula notepad.exe

Option 2

C:\PSTools\PsExec.exe notepad.exe
Start-Sleep -s 10
C:\PSTools\PsKill.exe -accepteula notepad.exe

Both options result in the same outcome, notepad is opened and then closed. The first option however calls notepad.exe from the command prompt.
It is worth noting that unless PsKill is passed the specific process ID for the notepad process opened, all notepad.exe processes will be killed.
To run this on a remote machine, you can use the following:

$remoteMachine = "tc-app01-s-vi"
$adminUserName = "TestUser"
$adminPassword = "TestPassword"
C:\PSTools\PsExec.exe \\$remoteMachine -u $adminUserName -p $adminPassword -i 1 cmd /c start notepad.exe
Start-Sleep -s 10
C:\PSTools\PsKill.exe \\$remoteMachine -u $adminUserName -p $adminPassword -accepteula notepad.exe

The only difference here being that we are connecting PsExec to a session on the remote machine as a specified user.
Using cmd /c also gives you the option of using “internal commands” such as CD and DIR that you would normally run via the command prompt.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.