# Kill Process on Remote Machine

**URL:** <https://forums.powershell.org/t/kill-process-on-remote-machine/4049>\
**Category:** PowerShell Help\
**Created:** [April 16, 2015, 4:33am UTC](https://forums.powershell.org/t/kill-process-on-remote-machine/4049 "2015-04-16T04:33:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jesse-cleary](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jesse-cleary](https://forums.powershell.org/u/jesse-cleary)\
**Post date:** [April 16, 2015, 4:33am UTC](https://forums.powershell.org/t/kill-process-on-remote-machine/4049/1 "2015-04-16T04:33:34Z")

</div>

I have a script I’m working on that asks for a machine name, and then produces a list of running processes on that machine. From there it asks the user I they want to kill a process on that machine. If they select Yes, it prompts the user to type in the name of the process and is supposed to kill it. Here is the code I have so far:

```
Get-Process -ComputerName $machine | Out-GridView
Write-Host
$message = Write-Host "Do you want to kill a process?" -foregroundcolor Green
Write-Host
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No"
$options = [System.Management.Automation.Host.ChoiceDescription[]][$yes, $no]
$result = $host.ui.PromptForChoice[$title, $message, $options, 0]
switch [$result]
{
    0{
    $process = Read-Host 'What is the process name?'
    Write-Host
    Write-Host "Killing Process..." -ForegroundColor Yellow
    Invoke-Command -ComputerName $machine {Stop-Process -ProcessName $process}

}
}
```

 ![](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/1/13e399a6f6d2be42f4d7188bec8350ad15e267ce.png)

I’m not sure what parameter called ‘Name’ it is referring to but my eyes have gone cross-eyed looking at this so maybe I’m missing something obvious?

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [April 16, 2015, 5:30am UTC](https://forums.powershell.org/t/kill-process-on-remote-machine/4049/2 "2015-04-16T05:30:18Z")

</div>

Please get into the habit of checking the parameters of cmdlets and functions with Get-Help. You won’t find a -ProcessName parameter. The correct parameters are -Name, -Id or -InputObject depending on the use case.

```
PS C:\> Get-Help Stop-Process

NAME
    Stop-Process

SYNTAX
    Stop-Process [-Id] [-PassThru] [-Force] [-WhatIf] [-Confirm] []

    Stop-Process -Name [-PassThru] [-Force] [-WhatIf] [-Confirm] []

    Stop-Process [-InputObject] [-PassThru] [-Force] [-WhatIf] [-Confirm] []
```

Another gotcha. The following line won’t work as expected:

```
Invoke-Command -ComputerName $machine {Stop-Process -ProcessName $process}
```

The curly brackets define a scriptblock. To get the value of variables outside of the scriptblock into it you will need to use the $Using: construct.

```
Invoke-Command -ComputerName $machine {Stop-Process -ProcessName $Using:process}
```

All the best to you,  
Daniel

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:45pm UTC](https://forums.powershell.org/t/kill-process-on-remote-machine/4049/3 "2024-05-16T20:45:27Z")

</div>

Closed on May 16, 2024, 8:45pm UTC


