Stop-ProcessOnComputer.ps1


Description

Purpose

Stops a specified process on one or more computers.

Detailed Description

The Stop-ProcessOnComputer function stops a specified process on one or more computers. It uses the CIM_Process class to find and stop the process.

Back to Top

Usage

Example 1

$cred = Get-Credential
“Server1”, “Server2” Stop-ProcessOnComputer -Name “Notepad” -Credential $cred This example stops the Notepad process on Server1 and Server2 using the provided credentials.

Example 2

Stop-ProcessOnComputer -ComputerName "Server1" -Name "Notepad" -Force

This example forcefully stops the Notepad process on Server1.

Back to Top

Notes

No additional notes.

Back to Top


Script

<#
.SYNOPSIS
Stops a specified process on one or more computers.

.DESCRIPTION
The Stop-ProcessOnComputer function stops a specified process on one or more computers. 
It uses the CIM_Process class to find and stop the process.

.PARAMETER ComputerName
The names of the computers where the process should be stopped. 
This parameter accepts pipeline input and can be used in a pipeline command.

.PARAMETER Name
The name of the process to stop. 
This parameter accepts pipeline input and can be used in a pipeline command.

.PARAMETER Force
If this switch is provided, the function will forcefully terminate the process.

.PARAMETER Credential
The credentials to use when connecting to the computers. 
If not provided, the function will use the current user's credentials.

.EXAMPLE
$cred = Get-Credential
"Server1", "Server2" | Stop-ProcessOnComputer -Name "Notepad" -Credential $cred

This example stops the Notepad process on Server1 and Server2 using the provided credentials.

.EXAMPLE
Stop-ProcessOnComputer -ComputerName "Server1" -Name "Notepad" -Force

This example forcefully stops the Notepad process on Server1.
#>
function Stop-ProcessOnComputer {
    [CmdletBinding(SupportsShouldProcess = $true, DefaultParameterSetName = 'Default')]
    param (
        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "The names of the computers where the process should be stopped.")]
        [ValidateNotNullOrEmpty()]
        [string[]]
        $ComputerName,

        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "The name of the process to stop.", ParameterSetName = 'Default')]
        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "The name of the process to stop.", ParameterSetName = 'Force')]
        [ValidateNotNullOrEmpty()]
        [string]
        $Name,

        [Parameter(HelpMessage = "If this switch is provided, the function will forcefully terminate the process.", ParameterSetName = 'Force')]
        [switch]
        $Force,

        [Parameter(HelpMessage = "The credentials to use when connecting to the computers. If not provided, the function will use the current user's credentials.")]
        [System.Management.Automation.PSCredential]
        $Credential
    )

    process {
        foreach ($Computer in $ComputerName) {
            if ($PSCmdlet.ShouldProcess("$Computer", "Stop process $Name")) {
                try {
                    $cimParams = @{
                        ClassName    = "Win32_Process"
                        Namespace    = "root/CIMV2"
                        ComputerName = "$Computer"
                    }
                    if ($null -ne $Credential) {
                        $cimParams.Credential = $Credential
                    }
                    $Process = Get-CimInstance @cimParams | Where-Object -Property Name -Like ($Name + ".exe")

                    if ($Process) {
                        $Process | ForEach-Object {
                            $result = Invoke-CimMethod -InputObject $_ -MethodName "Terminate"
                            [PSCustomObject]@{
                                ComputerName    = $Computer
                                ProcessName     = $_.Name
                                ProcessId       = $_.ProcessId
                                TerminateResult = $result.ReturnValue
                            }
                        }
                    }
                }
                catch {
                    Write-Error -Message "Failed to get process information from Server ${Computer}: $_"
                }
            }
        }
    }    
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top