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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Ben Personick (Previously QCubed)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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WeTiAuthor Commented:
works well
1
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Glad to help :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.