Home | Projects | Downloads | Links | Youtube! | Contact

Powershell Scripts!

Script to find and log of a user from one or multiple RDS/RDP sessions in your network. The way this works is just using 'quser' and 'logoff' respectively, but querying any amount of server names you specify and and finding their session ID. It will log them off and then give you the option to also delete their RDS profile disk (if you use profile disks) if you specify the path in the code below. Edit the workstations variable as well as your domain name, and RDS profile disk path
#3.26.24 RDS User Logoff script 1.1
#Baylin @ Molten Software

#log off user from any RDS session in your network by only entering username/saMaccountName

write-host "RDS User Logoff" -ForegroundColor Cyan
write-host "Enter the user's saMaccountName" -ForegroundColor Green
write-host "Examples: AThompson or Alex.Thompson"-ForegroundColor Green
write-host "CAN NOT be email or UPN like alex.thompson@molten.pw" -ForegroundColor Red

$userName = read-host "saMaccountName:"
#clear variables
$sessionId = ""
$found = ""
#edit for your domain name here
$domain = "molten"
#edit for your RDS profile path if you want to delete the user profile after logging them off.
$profilePath = "\\RDSprofiles\C$\Profiles"
#enter your RDS servers here, seperated by comma
$workstations =
"RDSServer1",
"RDSServer2",
"RDSServer3",
"RDSServer4",
"RDSServer5"

ForEach($Workstation In $Workstations){

write-host "Checking $workstation" -ForegroundColor cyan
quser /server:$workstation
$sessionId = ((quser /server:$workstation | Where-Object { $_ -match $userName }) -split ' +')[2]

if ($sessionId) {
write-host "Found user in $sessionId on $workstation" -ForegroundColor yellow
write-host "Logging user $username off from $workstation"-ForegroundColor yellow
logoff $sessionId /server:$workstation 
$found = "yes"
}

}


if ($found) {
$time = Get-Date
Write-Host "User $username was found on $workstation and was logged off at $time" -ForegroundColor Magenta
#reset $found variable
$found = ""
}
Write-Host "If you want to delete the user'profile as well, type 'RDSdel' and hit enter." -ForegroundColor Cyan


function RDSdel {
$objUser = New-Object System.Security.Principal.NTAccount("$domain", "$username")
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$deleteObject = $profilePath + "\UVHD-" + $strSID.Value + ".vhdx"

if (Test-Path -path $deleteObject) { 

rm $deleteObject -ErrorAction Stop
Write-Host "$username 's profile @ $deleteObject has been deleted." -ForegroundColor Green

}

else {Write-Host "$username 's RDS profile disk not found or recently deleted" -ForegroundColor yellow}
}

Example of script running:

Website created in FrontPage 2003. The only things I own on this site are my creations, and the Molten Software logo. 2003-2023 Molten Software. Everything else belongs to it's respective owners. Molten started as my basement computer repair lab back in high school, and although hasn't done much since, the name lives on.

SSL cert provided for modern web browsers.

Proud member of TheOldNet webring! Check some other cool websites!
[Previous site] - [Random site] - [Next site]