Skip to content

Open and close a process with Powershell

06/07/2016

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.

Advertisements

From → Powershell

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: