[CmdletBinding()] param( [string]$Destination, [string[]]$Folders = @('Desktop', 'Documents', 'Downloads', 'Pictures'), [switch]$Force ) $ErrorActionPreference = 'Stop' if (-not $Destination) { $Destination = Join-Path (Get-Location) ('profile-backup-' + (Get-Date -Format 'yyyyMMdd-HHmmss') + '.zip') } $Destination = [IO.Path]::GetFullPath($Destination) $parent = Split-Path -Parent $Destination if ($parent -and -not (Test-Path -LiteralPath $parent)) { New-Item -ItemType Directory -Path $parent | Out-Null } if ((Test-Path -LiteralPath $Destination) -and -not $Force) { throw "Файл уже существует: $Destination. Укажите другое имя или добавьте -Force." } $paths = New-Object System.Collections.Generic.List[string] foreach ($folder in $Folders) { $path = Join-Path $env:USERPROFILE $folder if (Test-Path -LiteralPath $path -PathType Container) { $paths.Add($path) Write-Host "В архив: $path" } else { Write-Warning "Папка не найдена и пропущена: $path" } } if ($paths.Count -eq 0) { throw 'Не найдено ни одной папки для архива. Проверьте -Folders.' } foreach ($path in $paths) { $prefix = $path.TrimEnd('\') + '\' if ($Destination.StartsWith($prefix, [StringComparison]::OrdinalIgnoreCase)) { throw "Архив нельзя сохранять внутрь архивируемой папки: $path" } } Write-Host "Создаю архив: $Destination" $pathArray = $paths.ToArray() if ($Force) { Compress-Archive -LiteralPath $pathArray -DestinationPath $Destination -CompressionLevel Optimal -Force } else { Compress-Archive -LiteralPath $pathArray -DestinationPath $Destination -CompressionLevel Optimal } $size = (Get-Item -LiteralPath $Destination).Length Write-Host ("Архив готов: {0} ({1} МБ)" -f $Destination, [math]::Round($size / 1MB, 2)) Write-Host 'Исходные папки не изменялись.'