Skip to content

Convert-Dir27z

Converts a directory into a .7z file

param([string]$dirPath = "", [string]$targetFile = "")

try {
	if (-not(Test-Path "C:\Program Files\7-Zip\7z.exe" -pathType leaf)) { throw "Please install 7-Zip" }
	if ($dirPath -eq "" ) { $dirPath = Read-Host "Enter the path to the folder" }

	$stopWatch = [system.diagnostics.stopwatch]::startNew()
	$dirPath = Resolve-Path $dirPath
	if ($targetFile -eq "" ) { $targetFile = "$dirPath.7z" }

	& "C:\Program Files\7-Zip\7z.exe" a -y -snl -t7z $targetFile $dirPath

	[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
	"? Converted ??$dirPath into $targetFile in $($elapsed)s."
	exit 0 # success
} catch {
	"?? ERROR: $($Error[0]) (script line $($_.InvocationInfo.ScriptLineNumber))"
	exit 1
}

Specifies the path to the directory (to be entered by default)

Specifies the path to the target file (<dirPath>.7z by default)

An interactive directory of PowerShell scripts.