獲得運行的應用名並賦給變量:javascript
$applications = gps | ? {$_.mainwindowhandle -ne 0} | select name
或者java
$applications = get-process | where-object {$_.mainwindowhandle -ne 0} | select-object name
經過"get-process"與判斷條件,篩選存在mainwindowhandle(主窗口句柄)的process,並選出它們的name,賦值給$applications。shell
$applications是一個數組。經過循環中止應用(此處不但願關閉powershell自身):數組
if ($applications.count -ne 0) { foreach ($application in $applications) { if ($application -ne $null -and $application.name -ne "powershell") { stop-process -force -name $application.name } } }
經過$applications的name屬性,去中止線程(stop-process)。app
補充,若是是操做遠程機器(name=$computername),則應該吧上述代碼放到$script變量中,並用線程
invoke-command -script $script -computername $computername
的方式去執行命令。code