Optimize Your FSLogix Environment: PowerShell Script for Deleting Inactive Profiles

1. Context:

1. There are 2 user profiles: user 1 and user 2. 

2. User 1 has recently logged in or is currently logged in. According to the script, User 1's VHDX file should be excluded.


3. User 2 has not logged in for the last 2 days. According to the script, the VHDX file and the associated directory should be deleted.



2. Prerequisites:

1. Ensure that the user ID you are using to run the script has access to the storage account. In this demo, the user ID admin@cloudazure.co.in is being used and has the following access:




2. The storage path must be accessible from the virtual machine (VM) where the script is being executed.


3. Modifications to be applied to the script:

$Date = (Get-Date).AddDays(-1) Change 1 to 30 days or any number of days based on your requirement.

$VHDs = Get-ChildItem -Recurse -Path "\\strdem07.file.core.windows.net\fileshare" | Where-Object { $_.Name -like "*.vhdx" } Change the path to your required path

4. Script:

# Import ActiveDirectory Module

Import-Module ActiveDirectory


# Set Date Variables

$FormatDate = Get-Date -Format "dd-MM-yyyy"

$Date = (Get-Date).AddDays(-2)


# Set $ErrorActionPreference for debugging

$old_ErrorActionPreference = $ErrorActionPreference

$ErrorActionPreference = "Continue"


# Ensure the report directory exists

$ReportDirectory = 'C:\Reports'

if (-not (Test-Path -Path $ReportDirectory)) {

    New-Item -ItemType Directory -Path $ReportDirectory | Out-Null

}


$ReportPath = "$ReportDirectory\Inactive_Profiles_$FormatDate.csv"

Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") - Exporting CSV to: $ReportPath"


# Import all VHD(X) files into $VHDs variable

$VHDs = Get-ChildItem -Recurse -Path "\\strdem07.file.core.windows.net\fileshare" | Where-Object { $_.Name -like "*.vhdx" }

Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") - Found $($VHDs.Count) VHDX files."


# Initialize an array for reports

$Reports = @()


ForEach ($VHD in $VHDs) {

    Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") - Processing $($VHD.Name)"

    If ($VHD.LastWriteTime -lt $Date) {

        Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") - Inactive profile detected: $($VHD.Name)"

        $VHDLWT = $VHD.LastWriteTime.ToString("dd-MM-yyyy")

        $VHDCT = $VHD.CreationTime.ToString("dd-MM-yyyy")

        $Username = ($VHD.Name).Split('_')[1] | ForEach-Object { $_.Substring(0, $_.Length - 5) }

        

        # Calculate Profile Size in GB

        $ProfileSizeGB = [math]::Round($VHD.Length / 1GB, 2)

        

        # Get User Details

        try {

            $UserDetails = Get-ADUser -Identity $Username -Properties DisplayName, Mail -ErrorAction Stop

        } catch {

            Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") - Failed to retrieve user details for $Username"

            $UserDetails = [PSCustomObject]@{ DisplayName = $null; SamAccountName = $null; Mail = $null }

        }

        

        # Create Custom Report Object

        $Report = [PSCustomObject]@{

            DisplayName = $UserDetails.DisplayName

            SamAccountName = $UserDetails.SamAccountName

            Email = $UserDetails.Mail

            ProfileName = $VHD.Name

            ProfileSizeGB = $ProfileSizeGB

            CreationTime = $VHDCT

            LastWriteTime = $VHDLWT

            ProfilePath = $VHD.FullName

        }


        # Add to reports array

        $Reports += $Report

        

        # Remove the file

        Remove-Item -Path $VHD.FullName -Force -Confirm:$false

        Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") - Profile $($VHD.Name) removed."


        # Check and remove the associated directory if empty

        $Directory = [System.IO.Path]::GetDirectoryName($VHD.FullName)

        if (-not (Get-ChildItem -Path $Directory -Recurse -Force)) {

            Remove-Item -Path $Directory -Recurse -Force

            Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") - Directory $Directory removed."

        }

    }

}


# Export the collected report data to a CSV file

$Reports | Export-Csv -Path $ReportPath -NoTypeInformation

Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") - Report exported to $ReportPath."


# Restore the original $ErrorActionPreference

$ErrorActionPreference = $old_ErrorActionPreference


5. Script Output:




6. CSV File output: