Check If RDP In Use
Code
| <#
.SYNOPSIS
Checks a remote machine to see if anyone is currently connected via RDP
.DESCRIPTION
When prompted, enter the machine name (IP should also work)
and it will tell you if anyone is currently connected via RDP
.NOTES
REVISION ID : 1
UPDATED : 2024-10-03
AUTHOR : Noxcivis
LICENSE : MIT
SOURCE : https://noxcivis.supportmarks.com
NOTES : No additional notes
LOCAL ADMIN : Not required
REMOTE ADMIN : YES
#>
Clear-Host
# Prompt the user to enter the machine name
$computerName = Read-Host "Enter the name of the machine you want to reboot"
# Check if the machine name is blank
if ([string]::IsNullOrWhiteSpace($computerName)) {
Write-Output "No machine name entered. Exiting script."
exit
}
# Get the list of RDP sessions
$sessions = Get-WmiObject -Class Win32_LogonSession -ComputerName $computerName |
Where-Object { $_.LogonType -eq 10 }
# Get the list of users associated with the RDP sessions
foreach ($session in $sessions) {
$user = Get-WmiObject -Class Win32_LoggedOnUser -ComputerName $computerName |
Where-Object { $_.Dependent -match $session.__RELPATH }
$userName = $user.Antecedent -replace '.*Domain="([^"]+)",Name="([^"]+)".*', '$1\$2'
Write-Output "User $userName is connected via RDP."
}
|
TIP
Save as check-if-rdp-in-use.ps1