Maintain single instance of start-process

679 Views Asked by At

I am using powershell on Windows 8. I am using this line of code...

start-process -filepath "C:\Program Files\Internet Explorer\iexplore.exe" -argumentlist "-k http://localhost/"

...to start IE. This works, however, when the event is triggered multiple times, multiple instances of kiosk IE are opened. How can I maintain a single instance of kiosk IE, and later close it programmatically?

Thank you! :)

1

There are 1 best solutions below

2
On

Would this work?

$ie = Get-Process -Name iexplore -ErrorAction SilentlyContinue

if ($ie -eq $null)
{
    start-process -filepath "C:\Program Files\Internet Explorer\iexplore.exe" -argumentlist "-k http://localhost/"
}

To stop the running instance you can do this:

$ie = Get-Process -Name iexplore -ErrorAction SilentlyContinue

if ($ie -ne $null)
{
    $ie | Stop-Process
}

EDIT: You can also combine the two to ensure IE is not running before you start your process and if it is, it will be stopped:

Get-Process -ErrorAction SilentlyContinue -Name iexplore | Stop-Process -ErrorAction SilentlyContinue
start-process -filepath "C:\Program Files\Internet Explorer\iexplore.exe" -argumentlist "-k http://localhost/"