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

Basic CSS: Use Clockwise Notation to Specify the Margin of 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: 20px 40px 20px 40px;}.red-box…

Basic HTML and HTML5: Create a Form Element

<h2>CatPhotoApp</h2><main><p>Click here to view more <a href="#">cat photos</a>.</p><a href="#"><img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying…

PowerShell: How To Test A Server Ephemeral Port

# Setup a listening port on server # This session will automatically terminates after a…