Remote_DisconnectSession.ps1

function Remote-DisconnectSession {
    $machineName = Read-Host -Prompt "Enter machine name"


    $existingSession = Get-PSSession | Where-Object { $_.ComputerName -eq $machineName }

    if ($existingSession -and $existingSession.State -eq "Opened") {
        Disconnect-PSSession -Session $existingSession.Id
        Write-Output "Disconnected from the session."
    }
    else {
        Write-Output "No open session found to disconnect from."
    }
}