Posted On December 25, 2019

PowerShell: Snippet to Detect and Disconnect Active PS Sessions

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Snippet to Detect and Disconnect Active PS Sessions
# Manual Detection

PS C:\Windows\system32> get-pssession
Id Name ComputerName ComputerType State ConfigurationName Availability
-- ---- ------------ ------------ ----- ----------------- ------------
6 Session6 SHERVER007 RemoteMachine Opened Microsoft.PowerShell Available
7 Session7 SHERVER007 RemoteMachine Opened Microsoft.PowerShell Available
# Disconnect sessions as a cleanup routine
$activeSessionIds=(Get-PSSession).Id
if($activeSessionIds){
Remove-PSSession -id $activeSessionIds;
write-host "Session(s) $activeSessionIds disconnected."
}

# Expected Output
Session(s) 6 7 disconnected.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

How to Restart Domino Services without Reboot

--------------Restart_Domino.bat------------------------------------------------------net stop "Lotus Domino Server (LotusDominoData)"c:\bats\pulist | findstr /I /C:"nadminp.exe" >c:\bats\pid.lstc:\bats\pulist | findstr /I /C:"naldaemn.exe"…

Linux: How to Set Startup Script

In previous Linux versions, startup scripts can simply be enabled by linking a script into…

Basic CSS: Add a Negative Margin to an Element

<style>.injected-text {margin-bottom: -25px;text-align: center;}.box {border-style: solid;border-color: black;border-width: 5px;text-align: center;}.yellow-box {background-color: yellow;padding: 10px;}.red-box {background-color: crimson;color: #fff;padding:…