Skip to content

Powershell – Find UPD Disk and RDHost

With User Profile Disks it sometimes so happens that due to a connection issue or whatever reason a user’s disk does not properly disconnect from the Remote Desktop Host. This can cause issues with for example File Explorer not functioning or loads of other nuisances.

Going through all the disks in the Disk Management to see which one belongs to which user takes some time. So I have made a Powershell script to find which disk on which host, run this on the broker:

$UserToFind = Read-Host "Type username to find (e.g. firstname.lastname)?"
$User = $env:USERDOMAIN + '\' + $UserToFind
$RDCollection = Get-RDSessionCollection | where {$_.ResourceType -eq 'Remote Desktop'} $RDHosts = Get-RDSessionHost -CollectionName $RDCollection.CollectionName | select SessionHost $Array = Invoke-Command -ComputerName $RDHosts.SessionHost -ScriptBlock { Get-Disk | select Location,DiskNumber | where {$_.Location -notmatch "Integrated"} } foreach ($VHD in $Array){ $DiskID = (Get-Item $VHD.Location).Name.Substring(5).Split(".")[0] $objSID = New-Object System.Security.Principal.SecurityIdentifier ($DiskID) $objUser = $objSID.Translate( [System.Security.Principal.NTAccount]) if ($objUser.Value -eq $User){ $result = "$($objUser.Value) has user disk number $($VHD.DiskNumber) on $($VHD.PSComputername)" }else{ $result = "$($User) does not appears to not have any disks connected to RDSH" } } $result

Next thing to do is to connect to the RDS Host and detach the VHD via the Disk Management.

Disk Management

Ultimately you could include it in the script to be done automatically instead of getting the “$result =”. But I personally prefer to do it manually 🙂

Invoke-Command -ComputerName $VHD.PSComputername -ScriptBlock { Dismount-VHD -DiskNumber $VHD.DiskNumber }

Happy scripting and stay safe!

Published inPowerShellServer 2016Server 2019Windows Server


  1. Nick Nick

    Hi Bud

    Tried your script however get the error below:

    I need you to be a savior here I have 8x RDSH servers in the farm using fslogix, the UPD disk gets mounted on them and to locate the locked UPD is a tedious exercise..

    PS C:\temp> .\find-lockedupd.ps1
    Enter Username (e.g. firstname.lastname)?: shane.smith
    Get-RDSessionHost : Cannot process argument transformation on parameter ‘CollectionName’. Cannot convert value to type
    At C:\temp\find-lockedupd.ps1:4 char:47
    + … et-RDSessionHost -CollectionName $RDCollection.CollectionName | selec …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Get-RDSessionHost], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-RDSessionHost

    Invoke-Command : Cannot validate argument on parameter ‘ComputerName’. The argument is null or empty. Provide an
    argument that is not null or empty, and then try the command again.
    At C:\temp\find-lockedupd.ps1:6 char:40
    + $Array = Invoke-Command -ComputerName $RDHosts.SessionHost -ScriptBl …
    + ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

    shane.smith does not have any user profile disks mounted in RDS
    Press Enter to continue…:

    Hope you can help


    • aschimpie aschimpie

      Hi Nick, to be fair I have not tried this script myself with FSLogix, assuming it should return the same. Did you run the script on the RDBroker? Or do you have multiple collections that serve ‘Remote Desktop’ ?

Leave a Reply

Your email address will not be published.