How to Programmatically List All NTFS Compressed Files and Folders on Windows Server 2003 Using PowerShell


5 views

Working with legacy Windows Server 2003 systems presents unique challenges when trying to identify compressed files. The NTFS file system marks compressed files with a specific attribute flag, but Windows Explorer's search functionality doesn't provide direct filtering for this property.

Here's a PowerShell script that recursively scans directories and outputs compressed items to a text file. This works because NTFS compression sets the Archive file attribute's compression bit (0x800):


# CompressedFilesFinder.ps1
$outputFile = "C:\temp\compressed_files_report.txt"
$searchPath = "C:\"

Get-ChildItem -Path $searchPath -Recurse -Force | Where-Object {
    $_.Attributes -match "Compressed"
} | ForEach-Object {
    $_.FullName
} | Out-File -FilePath $outputFile

Write-Host "Compressed files report generated at $outputFile"

For a more comprehensive report including size savings:


$report = @()
Get-ChildItem -Path $searchPath -Recurse -Force | Where-Object {
    $_.Attributes -match "Compressed"
} | ForEach-Object {
    $report += New-Object PSObject -Property @{
        Path = $_.FullName
        Name = $_.Name
        Size = "{0:N2} MB" -f ($_.Length / 1MB)
        Type = $_.GetType().Name
        LastModified = $_.LastWriteTime
    }
}

$report | Export-Csv -Path "C:\temp\compressed_files_detail.csv" -NoTypeInformation

Windows includes a command-line utility that can report compression status:


compact /s:$searchPath /q > compressed_files_report.txt

For servers with millions of files, consider this optimized approach:


$startTime = Get-Date
Write-Host "Starting compressed files scan at $startTime"

$counter = 0
$compressedCount = 0

Get-ChildItem -Path $searchPath -Recurse -Force | ForEach-Object {
    $counter++
    if ($counter % 1000 -eq 0) {
        Write-Progress -Activity "Scanning files" -Status "$counter files processed" -PercentComplete ($counter % 100)
    }
    if ($_.Attributes -match "Compressed") {
        $compressedCount++
        $_.FullName | Out-File -FilePath $outputFile -Append
    }
}

$endTime = Get-Date
Write-Host "Scan completed at $endTime"
Write-Host "Total files processed: $counter"
Write-Host "Compressed files found: $compressedCount"

The blue color in Windows Explorer corresponds to the FILE_ATTRIBUTE_COMPRESSED flag (0x800) in the file attributes DWORD. PowerShell's Get-ChildItem cmdlet exposes this through the Attributes property, where we check for the "Compressed" string match.

Always include proper error handling for permission issues and long paths:


Get-ChildItem -Path $searchPath -Recurse -Force -ErrorAction SilentlyContinue -ErrorVariable accessErrors | Where-Object {
    $_.Attributes -match "Compressed"
} | ForEach-Object {
    $_.FullName
} | Out-File -FilePath $outputFile

if ($accessErrors) {
    $accessErrors | Out-File -FilePath "C:\temp\compressed_files_errors.log"
}

When dealing with Windows Server file systems, identifying compressed files isn't as straightforward as a simple Explorer search. The blue-colored files in Windows Explorer indicate files with the NTFS compression attribute set. This compression is different from archive formats like ZIP or RAR - it's a filesystem-level compression handled by NTFS.

Here's a comprehensive PowerShell script that will recursively scan directories and output all compressed files to a text file with their full paths:


# Define output file path
$outputFile = "C:\temp\compressed_files_report.txt"

# Get all compressed files recursively from C: drive
$compressedFiles = Get-ChildItem -Path C:\ -Recurse -Force -ErrorAction SilentlyContinue | 
    Where-Object { $_.Attributes -band [System.IO.FileAttributes]::Compressed }

# Export results
$compressedFiles | Select-Object FullName | Out-File -FilePath $outputFile

Write-Host "Found $($compressedFiles.Count) compressed files. Report saved to $outputFile"

For more targeted searches, you can modify the script with these variations:


# Search only specific folders
Get-ChildItem -Path "C:\ImportantFolder" -Recurse | 
    Where-Object { $_.Attributes -match "Compressed" }

# Include only files above certain size
Get-ChildItem -Path C:\ -Recurse -Force | 
    Where-Object { ($_.Attributes -band [System.IO.FileAttributes]::Compressed) -and ($_.Length -gt 10MB) }

# Export with additional details
Get-ChildItem -Path C:\ -Recurse -Force | 
    Where-Object { $_.Attributes -band [System.IO.FileAttributes]::Compressed } |
    Select-Object FullName, Length, LastWriteTime |
    Export-Csv -Path "compressed_files_details.csv" -NoTypeInformation

When scanning entire servers:

  • Run scripts during off-peak hours
  • Consider limiting depth with -Depth parameter in newer PowerShell versions
  • Add error handling for inaccessible folders
  • For very large volumes, process in batches

For systems where PowerShell isn't available, you can use this command prompt solution:


compact /s /a C:\ > compressed_files_report.txt

This will generate a report of all compressed files, though the output format requires more parsing than the PowerShell solution.

Create a scheduled task that runs this script weekly and emails the results:


# PowerShell script with email functionality
$compressedFiles = Get-ChildItem -Path C:\ -Recurse -Force | 
    Where-Object { $_.Attributes -band [System.IO.FileAttributes]::Compressed }

$body = $compressedFiles | Select-Object FullName, Length | ConvertTo-Html

Send-MailMessage -From "server@domain.com" -To "admin@domain.com" 
    -Subject "Weekly Compressed Files Report" -Body $body -BodyAsHtml 
    -SmtpServer "smtp.domain.com"