[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2730
  • Last Modified:

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.
1
SiHodgy007
Asked:
SiHodgy007
  • 7
  • 6
1 Solution
 
Chris DentPowerShell DeveloperCommented:

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
0
 
SiHodgy007Author Commented:
I was thinking of using an environmental variable to call 7-zip but need to do it neatly and efficiently!
0
 
Chris DentPowerShell DeveloperCommented:

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
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
Chris DentPowerShell DeveloperCommented:

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

0
 
SiHodgy007Author Commented:
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!
0
 
Chris DentPowerShell DeveloperCommented:

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
0
 
SiHodgy007Author Commented:
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
0
 
Chris DentPowerShell DeveloperCommented:

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
0
 
SiHodgy007Author Commented:
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
0
 
Chris DentPowerShell DeveloperCommented:

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

Chris
0
 
SiHodgy007Author Commented:
$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"
}
0
 
SiHodgy007Author Commented:
$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"
}
0
 
Chris DentPowerShell DeveloperCommented:

Ahh okay sorry about that we need a $ and a couple of brackets. That makes it get the value for full name before combining it with the rest of the string.

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

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now