Link to home
Start Free TrialLog in
Avatar of SiHodgy007
SiHodgy007Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Powershell 7-zip

I need a way of calling 7-zip without calling an external shell.

 All I want to do is zip a file from one location and out it in another using powershell.
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image


What do you mean without calling an external shell?

7-zip appears to have command line options that allow you to add files to an archive easily enough. There's no reason you can't use that executable within PowerShell.

Chris
Avatar of SiHodgy007

ASKER

I was thinking of using an environmental variable to call 7-zip but need to do it neatly and efficiently!

With $Env:<VariableName>? Otherwise you'd be right in thinking you'd have to give it the full path to the executable.

Could you show us what you're trying at the moment?

Chris

This works for me if it's any help at all. Note the quoting in the path, it can get quite picky about where you put those quotes.

Chris
$7Zip = 'C:\"Program Files"\7-Zip\7z.exe'
$Archive = "test.7z"
$Path = "C:\Test"
 
Get-ChildItem $Path | ?{ $_.Name -like "*.txt" } | %{
  Invoke-Expression "$7Zip a $Archive $_.FullName"
}

Open in new window

Is this the simplest way of calling 7-zip? when you know the file you are collecting and the destination you are going to. I would like it simpler!

You have to know those things and tell the script at some point, how would you like to feed it to them? :)

If you had the values stored in environmental variables it could pick them from there. Or if you wanted it to find something specific on the file system it could do that as well.

Chris
When I mean simple I mean like

$7Zip = 'C:\"Program Files"\7-Zip\7z.exe'
$Archive = "c:\test.7z"
$Path = "C:\Test.txt"
 

 Invoke-Expression "$7Zip a $Archive $Path

Sure you can do that, if it's all you need to archive :) The loop was only as an example to show you could have it add multiple things into the archive without much bother.

Chris
I get the following error when I run your script with with txt file in c:\test

PS C:\Test> .\script.ps1

7-Zip 4.65  Copyright (c) 1999-2009 Igor Pavlov  2009-02-03
Scanning


test.txt.FullName:  WARNING: The system cannot find the file specified.



Creating archive test.7z



WARNINGS for files:

test.txt.FullName : The system cannot find the file specified.

----------------
WARNING: Cannot find 1 file

Show me the script you have? It's adding on a .FullName value there for some reason.

Chris
$7Zip = 'C:\"Program Files"\7-Zip\7z.exe'
$Archive = "test.7z"
$Path = "C:\Test"
 
Get-ChildItem $Path | ?{ $_.Name -like "*.txt" } | %{
  Invoke-Expression "$7Zip a $Archive $_.FullName"
}
$7Zip = 'C:\"Program Files"\7-Zip\7z.exe'
$Archive = "c:\test\test.7z"
$Path = "C:\Test"
 
Get-ChildItem $Path | ?{ $_.Name -like "*.txt" } | %{
  Invoke-Expression "$7Zip a $Archive $_.FullName"
}
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial