I would like to stop/kill a certain process and then start it again after I am done doing what I have to do.
This is what I already have.
Clear-host
$processes = Get-Process devenv
$processes.Count
if($processes.Count -gt 1)
{
$i = 0
Write-host "There are multiple processes for devenv."
foreach($process in $processes)
{
$i++
$i.ToString() + '. ' + $process.MainWindowTitle
}
$in = Read-host "Give a number of the process to kill: "
write-host
write-host "killing and restarting: " + $processes[$in-1].MainWindowTitle
$processes[$in-1].Kill()
$processes[$in-1].WaitForExit()
$processes[$in-1].Start()
}
else
{
write-host "something else"
}
But the Start needs some parameter which I thought I could get from the process. But I’m not really sure I know what to give it.
The
$processes[$in-1].Start()will not work. You need to capture the processinfo you are killing and start the same app again. You can get the process binary and commandline information using Win32_Process WMI class.For example,
In the above example, I am using WMI to get the commandline information for a process selected. If that were a notepad process with some open text file, the commandline for that process would look like
"C:\WINDOWS\system32\NOTEPAD.EXE" C:\Users\ravikanth_chaganti\Desktop\debug.logNow, all you need to do is: Invoke that commandline somehow (this part is not there in example I wrote). A very blunt way to do that is:
But, in your case, there may not be any argument list.
Hope this gives you an idea. Other PowerShell experts may have a different & efficient approach. This is just a quick hack.