While likely to work (I don't use powershell), I think you missed the main constraint posed: kill a running GUI with a certain name.
Your solution finds the process to kill only if the executable path is at a known location.
How would you do this if you only know what the process name will be -- e.g., what if the executable path is on D:\stuff\gui.exe on host1 and E:\secretstuff\gui.exe on host2, if gui.exe always runs as a process named "Updatable GUI Thing"?
Silly me, I assumed that the executable path was the requirement. My bad.
If you need to find a process just by it's name, it is even simpler: Just indicate the process name (possibly with wildcards) to Get-Process (alias ps):
ps Notepad++ | kill
Actually, kill (alias for Stop-Process) takes a name parameter directly, so the "kill" line from the script could be written as simply
kill notepad++
But your question is actually really good, because what if we did not know the neither the process name nor the executable, but -say- only the Window title?
Get-process (alias ps) will produce a sequence objects describing the running processes.
If I want to know what properties those objects have that I can possibly filter on, I
can pipe the objects through the Get-Member cmdlet (alias gm):
ps | gm
This produces a table-formatted list like this (shortened):
Lo and behold, there is a property called MainWindowTitle. So to stop a process by it's main window title I could write:
ps | ? MainWindowTitle -eq 'Deepthought Main Console' | kill
That is, find all processes, pipe them through a filter selecting only those where the MainWindowTitle equals the desired text, and pipe those processes to the Stop-Process cmdlet
Your solution finds the process to kill only if the executable path is at a known location.
How would you do this if you only know what the process name will be -- e.g., what if the executable path is on D:\stuff\gui.exe on host1 and E:\secretstuff\gui.exe on host2, if gui.exe always runs as a process named "Updatable GUI Thing"?