Functions/Stop-ProcessSoft.ps1


Class Processes : System.Management.Automation.IValidateSetValuesGenerator {
    [string[]] GetValidValues() {

        $Processes = Get-Process

        $ProcessNames = $Processes.Name

        return [string[]] $ProcessNames

    }
}

function Stop-ProcessSoft {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        # [ArgumentCompleter( {
        # param ( $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters )
        # Get-Process "$wordToComplete*" | ForEach-Object { if ($_.Name -match " ") {
        # "'$($_.Name)'"
        # } else {
        # $_.Name
        # }
        # } }
        # )]
        [string] $ProcessName,
        [Parameter()] [switch] $Silent
    )

    if (!(Get-Process $ProcessName -ErrorAction 0)) {
        if (-not($Silent)) {
            Write-Warning "Process $($ProcessName) not found"
        }
    } else {
        1..100 | ForEach-Object {
            $Process = (Get-Process $ProcessName -ErrorAction 0)
            if ($Process) {
                $Process.CloseMainWindow() | Out-Null
            }
        }
    }

}


$scriptblock = {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
    $ps = Get-Process | Where-Object { $_.Name -like "$wordToComplete*" }
    $ps.Name | ForEach-Object {
        if ($_ -match " ") {
            "'$_'"
        } else {
            $_
        }
    }
}
Register-ArgumentCompleter -CommandName Stop-ProcessSoft -ParameterName ProcessName -ScriptBlock $scriptblock