Remove-xRDS_UserProfileDisk.ps1

function Remove-xRDS_UserProfileDisk {

<#
    .DESCRIPTION
    Removing orphan User Profile Disks from RDS Session Hosts.
  
    .PARAMETER Broker
    -ConnectionBroker - FQDN of RDS ConnectionBroker.
 
    .PARAMETER Broker
    -Computer - FQDN of RDS computer. (No required if you use connection brokers FQDN).
 
    .PARAMETER Credential
    -Credential [Optional] - Query RDS Connection Broker resources under provided credentials, the same credentials will be used to access RDS session hosts.
 
    .PARAMETER UI
    -Logfile [Optional] - Output deleted User Profile Disks objects into logfile. (Ignored if used with WhatIf option).
 
    .PARAMETER UI
    -UI [Optional] - Displays records in GridView for output selection.
 
    .PARAMETER WhatIf
    -WhatIf [Optional] - Displays orphan User Profile Disks records without deletion.
 
    .PARAMETER WINRMPort
    -WINRMPort [Optional] - WINRM Port to test for host connectivity validation. Default port is 5985.
 
    .EXAMPLE
    # Starts deletion of orphan User Profile Disks from RDS Session Hosts:
    Remove-xRDS_UserProfileDisk -ConnectionBroker ardscbl01.adatum.labnet
#>



    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$false)][string]$ConnectionBroker,
        [String]$Computer = "localhost",
        [String]$Logfile,
        [switch]$UI,
        [switch]$WhatIf,
        [Int]$WinRMPort = 5985,    
        [PSCredential]$Credential
    )      
   
    $ObjectsList=@()

    if($ConnectionBroker) {$Collection = Get-xRDS_CollectionsList -ConnectionBroker $ConnectionBroker -Credential $Credential} 
    else {$Collection = @{ $Computer = "Computer"}}

    Try {  

    #List session host maintenance mode

             foreach ($key in $Collection.Keys) 
                {

                $TempObject=@()
                $connection = $null;

                #Test host WInRM access
                $connection =  Invoke-xRDS_TestPort -hostname $key -port $WinRMPort
                
                if ($connection.open) {
                write-host "Removing user disk profiles from $key" -ForegroundColor Cyan
                
                    if($Credential) {
                    $ObjectsList += Invoke-Command -ComputerName $key -ArgumentList $WhatIf -Credential $Credential -ScriptBlock { `
                    $WhatIf = $args[0];
                    $excludefolders = @('Public','Default','UvhdCleanupBin') ;
                    $excludefolders +=  (Get-CimInstance -ClassName win32_userprofile | select LocalPath).LocalPath -replace '.*\\' ; 
                    Get-ChildItem C:\Users\ -Exclude $excludefolders | select FullName | foreach ($_) {if(!($WhatIf)){remove-item $_.fullname -Force -Recurse} ; `
                    (new-object -Type PSObject -Property @{Time = Get-Date -Format "yyyy-MM-dd_MM-mm" ; Profile = $_.fullname})}}
                    }
                    else {
                    $ObjectsList += Invoke-Command -ComputerName $key -ArgumentList $WhatIf -ScriptBlock { `
                    $WhatIf = $args[0];
                    $excludefolders = @('Public','Default','UvhdCleanupBin') ;
                    $excludefolders +=  (Get-CimInstance -ClassName win32_userprofile | select LocalPath).LocalPath -replace '.*\\' ; 
                    Get-ChildItem C:\Users\ -Exclude $excludefolders | select FullName | foreach ($_) {if(!($WhatIf)){remove-item $_.fullname -Force -Recurse} ; `
                    (new-object -Type PSObject -Property @{Time = Get-Date -Format "yyyy-MM-dd_MM-mm" ; Profile = $_.fullname})}}
                    }

                }   

                else {write-host "Cannot access WinRM port for $key" -ForegroundColor Red}                   
                } 

             #Output
             if($WhatIf) {write-host "User Profile Disks records have been listed without deletion." -ForegroundColor Magenta} 
             else {write-host "User Profile Disks records have been deleted." -ForegroundColor Yellow}

             if($logfile -and (!($WhatIf))){Write-host "Writing outout into log file $logfile";$ObjectsList |  Select-Object PSComputerName,Time,Profile | Export-Csv  $logfile -Append -NoTypeInformation }

             If($UI) {$ObjectsList | Out-GridView -PassThru -Title "RDS User Profile Disk List"}
             ELSE {$ObjectsList | ft } 

    } Catch {Write-host $_.Exception.message }   

}