public/Get-PSProfileSnapshot.ps1
|
<# .SYNOPSIS Lists all snapshots of the current user's profile with metadata. .DESCRIPTION Retrieves information about all stored profile snapshots, including timestamp, hash, file size, and any associated metadata. Snapshots are sorted by creation time in descending order (newest first). .INPUTS None .OUTPUTS System.Management.Automation.PSCustomObject Returns custom objects with the following properties: - Name: The snapshot name (custom name or timestamp-based) - Path: Full path to the backup file - Hash: The SHA256 hash value - FileSize: Size of the backup file in bytes - CreatedTime: When the snapshot was created - Notes: User-provided notes (if any) .EXAMPLE Get-PSProfileSnapshot Lists all profile snapshots with their metadata. .EXAMPLE Get-PSProfileSnapshot | Format-Table -AutoSize Displays snapshots in a formatted table. .NOTES Snapshots are identified by their .backup and .hash file pairs. Snapshots without both files are considered incomplete. #> function Get-PSProfileSnapshot { [CmdletBinding()] param() try { $profilesPath = Split-Path -Path $profile -Parent $backupFiles = Get-ChildItem -Path $profilesPath -Filter "*.backup" -ErrorAction Stop if (-not $backupFiles) { Write-Host "ℹ️ No profile snapshots found." -ForegroundColor Cyan return } $snapshots = @() foreach ($backupFile in $backupFiles) { # Extract the GUID from the filename (format: profilename_GUID.backup) $guid = $backupFile.BaseName -replace '^.*_([a-f0-9\-]+)$', '$1' $hashFileName = "$($profile)_$($guid).hash" $metadataFileName = "$($profile)_$($guid).metadata" # Check if hash file exists if (Test-Path -Path $hashFileName -ErrorAction SilentlyContinue) { $hashContent = Get-Content -Path $hashFileName -ErrorAction SilentlyContinue # Try to load metadata if it exists $metadata = $null if (Test-Path -Path $metadataFileName -ErrorAction SilentlyContinue) { try { $metadata = Get-Content -Path $metadataFileName -Raw | ConvertFrom-Json -ErrorAction SilentlyContinue } catch { # Metadata file is corrupted or invalid JSON, continue without it } } $snapshotObject = [PSCustomObject]@{ Name = $metadata.Name ?? $backupFile.CreationTime.ToString("yyyy-MM-dd HH:mm:ss") GUID = [guid]$guid Path = $backupFile.FullName Hash = $hashContent ?? "N/A" FileSize = "$([math]::Round($backupFile.Length / 1KB, 2)) KB" CreatedTime = $backupFile.CreationTime Notes = $metadata.Notes ?? $null } $snapshots += $snapshotObject } } # Sort by creation time, newest first $snapshots = $snapshots | Sort-Object -Property CreatedTime -Descending # Display results if ($snapshots.Count -gt 0) { Write-Host "✅ Found $($snapshots.Count) profile snapshot(s):" -ForegroundColor Green } else { Write-Host "ℹ️ No valid profile snapshots found (missing hash files)." -ForegroundColor Cyan } return $snapshots } catch { Write-Error "❌ Could not retrieve profile snapshots." throw $_.Exception.Message } } |