# Define the file paths $downloadsPath = [Environment]::GetFolderPath('MyDocuments') -replace 'Documents$', 'Downloads' $files = @( "$downloadsPath\Disabled_Users_last_3_Days.txt", "$downloadsPath\Disabled_Users_last_7_Days.txt", "$downloadsPath\Disabled_Users.txt", "$downloadsPath\Expired_Users.txt", "$downloadsPath\Expired_Users_Last_3_Days.txt", "$downloadsPath\Expired_Users_Last_7_Days.txt", "$downloadsPath\inactive_users_30_days.txt", "$downloadsPath\inactive_users_90_days.txt" ) # Loop through each file and generate individual HTML reports foreach ($file in $files) { if (Test-Path $file) { # Import the CSV file $users = Import-Csv -Path $file # Check which column exists and dynamically set the header $dateColumn = if ($users[0].PSObject.Properties['DisabledDate']) { 'DisabledDate' } elseif ($users[0].PSObject.Properties['ExpirationDate']) { 'ExpirationDate' } elseif ($users[0].PSObject.Properties['LastLogonDate']) { 'LastLogonDate' } else { 'N/A' } # Create the HTML report $reportName = [System.IO.Path]::GetFileNameWithoutExtension($file) + ".html" $htmlFilePath = "$downloadsPath\$reportName" $htmlContent = @" Report: $reportName

Report: $reportName

"@ $users | ForEach-Object { $dateValue = if ($_.PSObject.Properties[$dateColumn]) { $_.PSObject.Properties[$dateColumn].Value } else { $null } $htmlContent += "" } $htmlContent += @"
Name SamAccountName EmailAddress $dateColumn FormattedOU
$($_.Name)$($_.SamAccountName)$($_.EmailAddress)$dateValue$($_.FormattedOU)
"@ # Save the HTML report $htmlContent | Set-Content -Path $htmlFilePath -Encoding UTF8 Write-Host "HTML report generated at $htmlFilePath" } else { Write-Host "File not found: $file" } } # Optional: Open the last HTML report in the default web browser if ($htmlFilePath) { Start-Process $htmlFilePath }