You snippet does not work because you are first actaully getting process on the remote computer and piping them to stop-process in local computer. Instead use invoke-command as @Olaf
Invoke-Command -ComputerName AD-DC01 -ScriptBlock {Get-Process -Name 'conhost', 'PowerShell' | Stop-Process}
you might have to use the -Authentication Kerberos option to gain rights to the remote computer. Also you can use psexec along with taskkill.
You snippet does not work because you are first actaully getting process on the remote computer and piping them to stop-process in local computer. Instead use invoke-command as @Olaf
Invoke-Command -ComputerName AD-DC01 -ScriptBlock {Get-Process -Name 'conhost', 'PowerShell' | Stop-Process}
you might have to use the -Authentication Kerberos option to gain rights to the remote computer. Also you can use psexec along with taskkill.
You can use WMI with the Cim cmdlets to do it. Something like:
Get-CimInstance -ComputerName ServerName -ClassName win32_Process -Filter "NAME = 'PowerShell.exe' OR NAME = 'conhost.exe'" |
Invoke-CimMethod -MethodName Terminate
You may want to play with the filter though.
Also, you can invoke directly without using Get-CimInstance First
Invoke-CimMethod -ComputerName ServerName -Query "SELECT * FROM Win32_Process WHERE Name = 'powershell.exe' OR Name = 'conhost.exe'" -MethodName Terminate
If you still want to use remoting, and to answer your previous question you don't need to call PowerShell when remoting, it would look something like:
Invoke-Command -ComputerName ServerName -ScriptBlock { Get-Process -Name 'conhost', 'PowerShell' -ComputerName 'AD-DC01' | Stop-Process }
How-to kill process remotely - IT & Tech Careers - Spiceworks Community
powershell - Kill a process on multiple remote machines - Stack Overflow
Kill Process on Remote Machine
wmi - Try to remotely kill a process using PowerShell - Stack Overflow
There are several approaches you can take to remote kill processes from a CLI:
Powershell
Assuming you have an account with the requisite permissions, and have configured Powershell for remote use (not covered in this answer, but here's a free e-book from Don Jones covering how to get set up,) you can use one of several Cmdlets to remotely kill processes.
Stop-Process via Invoke-Command
You should be able to use Stop-Process along with an Invoke-Command (or by opening a more permanent remote session).
Invoke-Command -ComputerName RemoteComputer -ScriptBlock {Stop-Process processname}
This would be my preference, but requires some configuration in advance, so is not ideal in every situation.
Built-in Solutions
Taskkill.exe
Taskkill is provided on recent Windows machines, and can be used remotely with the /s parameter.
Example:
taskkill /s remotecomputer /pid processID
Sysinternals Tools
You can also use either of PSKill or PSExec (available at live.sysinternals.com) to terminate processes.
PSKill
Similar to Taskkill, but not provided on Windows machines by default.
Example:
pskill \\remotecomputer <process ID | name>
PSExec
Using PSExec, on the other hand, you can run any command you would normally use to manage processes locally.
Example:
psexec \\remotecomputer taskkill /pid processID
You can run this command from cmd or the start menu:
taskkill /f /im name.exe
This also has a /S parameter to allow you to set the system to connect to. So you will be able to:
taskkill /s remoteserver /f /im name.exe
To find name.exe,
tasklist
will give you a chart with all the processes, the names, the executable (name.exe) and the PID [process ID].
If you have a text file with a list of machines you could do it trivially with:
get-content serverlist.txt | Foreach-object {& pskill -t \\$_ -u -p name.exe}
There are many methods to do this in Powershell like WMI, Invoke-command etc. Here is an example to do it using WMI:
$cred = get-credential
It will pop-up for Credential. This way you can make sure that credential is not visible in the script.
(Get-Content 'c:\Temp\Computers.txt') | ForEach-Object {
Get-WmiObject -computer $_ -class win32_process -filter "name = 'name.exe'" -credential $cred| %{$_.terminate()} | out-null
}