PS control 7zip

Dear Expert

How do I control 7-zip using Powershell? Im trying to do this below but it doesn't work.

[code]
$FromPath="\\mgmt01\d$\Test\*"
$temp = Get-ChildItem -Recurse -Path $FromPath | Where-Object { $_.Extension -eq "*.*" }
if (-not ("$env:C:\Program Files\7-Zip\7z.exe")) {throw "$env:Program Files (x86)\7-Zip\7z.exe needed"}
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe "
foreach ($file in $temp) {
$name = $file.name
$directory = $file.DirectoryName  
sz a -t7z "$FromPath" archieve.zip"$temp" -mx=5 -v100m
}[(/code]
LVL 1
WeTiAsked:
Who is Participating?
 
Ben Personick (Previously QCubed)Connect With a Mentor Lead Network EngineerCommented:
actually, I was more interested in fixing some of the Powershell stuff, but I see the 7Zip syntax seems off.

First off you seem to be putting the zip file in the same directory you're creating the zip of... is that what you really want?

Usually, you want your zip in a separate folder so you don't keep zipping the zip in the future.

 Second off you are using a for-loop to put all of the files into the zip, however, 7zip can zip all files and folders in a given directory without using a for-loop.

 if you want to zip all files and directories inside some certain directory you can use simply:

7z.exe a -bd -tZip "C:\Ful\lPath\Of\Zip\File.zip" "\\System\Share\Parth\To\Root\Folder"

As powershell:

$7ZipCmd="$($env:ProgramFiles)\7-Zip\7z.exe"
$FromPath="\\mgmt01\d$\Test"
$ZipPath="$($env:Temp)\TestArchive.zip"
if ([System.IO.File]::Exists($7ZipCmd)) {
	& $7ZipCmd a -t7z -mx=5 -v100m $ZipPath $FromPath
} else {
	write-error "Not Found: [$7ZipCmd]  -- Please install 7Zip"
}

Open in new window


I also noticed now that you had a \* in your from the path before, which would definitely not work in the loop or the Zip commands you wanted to use anyway.
0
 
Shaun VermaakTechnical Specialist/DeveloperCommented:
Are you using PowerShell 5? You could use Compress-Archive instead of 7z.exe
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
this should do the needful, mind you I don't have 7z installed on this system so if some syntax issue I can try again on another machine.

$7ZipCmd="$($env:ProgramFiles)\7-Zip\7z.exe"
$FromPath="\\mgmt01\d$\Test\*"
if ([System.IO.File]::Exists($7ZipCmd)) {
	$temp = Get-ChildItem -Recurse -Path $FromPath | Where-Object { $_.Extension -eq "*.*" }
	foreach ($file in $temp) { 
		$name = $file.name 
		$directory = $file.DirectoryName  
		& $7ZipCmd a -t7z $FromPath archieve.zip $file -mx=5 -v100m
	}
} else {
	write-error "Not Found: [$7ZipCmd]  -- Please install 7Zip"
}

Open in new window

0
 
WeTiAuthor Commented:
works well
1
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Glad to help :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.