Public/Stop-MusicForProgramming.ps1

function Stop-MusicForProgramming {
    <#
    .SYNOPSIS
        Stops the currently tracked Music For Programming playback process.

    .DESCRIPTION
        Stops the last background playback process started by Start-MusicForProgramming
        when it is trackable on the current platform/player.

        If playback was started with an untracked handler (for example `open` or
        `xdg-open`), this command will explain that there is no process to stop.

    .PARAMETER Force
        Forcefully terminate the tracked process.

    .EXAMPLE
        Stop-MusicForProgramming

        Stops the active tracked playback process.

    .EXAMPLE
        Stop-MusicForProgramming -Force

        Force-stops the tracked playback process.
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
    [OutputType([string])]
    param(
        [Parameter()]
        [switch]$Force
    )

    if (-not $script:CurrentPlayback) {
        $msg = 'No tracked Music For Programming playback process is active.'
        Write-Warning $msg
        return $msg
    }

    $pid = $script:CurrentPlayback.ProcessId
    $proc = Get-Process -Id $pid -ErrorAction SilentlyContinue

    if (-not $proc) {
        Clear-MFPPlaybackState
        $msg = "Tracked playback process $pid is no longer running."
        Write-Warning $msg
        return $msg
    }

    if ($PSCmdlet.ShouldProcess("PID $pid", 'Stop playback')) {
        try {
            Stop-Process -Id $pid -Force:$Force
            Clear-MFPPlaybackState
            $msg = "Stopped Music For Programming playback (PID $pid)."
            Write-Host $msg
            return $msg
        }
        catch {
            throw "Failed to stop playback process ${pid}: $_"
        }
    }
}