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

LAMPP Passwords Change

Change ProFTP password: vim /opt/lampp/etc/proftpd.conf <?-- delete the last line in password, leave the first…

Basic JavaScript: Manipulate Arrays With shift()

// Setupvar myArray = [["John", 23], ["dog", 3]];// Only change code below this line.var removedFromMyArray=myArray.shift();

Microsoft Dynamics – Selecting an Org

# These two functions are convenient for CRM admins to quickly gather a hosted organization…