Link to home
Start Free TrialLog in
Avatar of Isaiah Melendez
Isaiah Melendez

asked on

Powershell 7zip extract and Move

Dear Experts,

Could you provide an example of extracting .7z files with 7zip and moving the extracted all .7z files to a network directory?

.7z directory = C:\source

move extracted files directory = \\servername\c$\destination

Doing all this via powershell 3.0?
Avatar of Greg Besso
Greg Besso
Flag of United States of America image

I haven't used 7zip myself from within PowerShell (yet) but it seems you do have to do some special steps for that. Check this article out for some example of how to call it...

https://gallery.technet.microsoft.com/scriptcenter/PowerShell-and-7Zip-83020e74

From there on it's just Move-Item (or Get-ChildItem -Recurse "somewhere" | Move-Item "toSomewhereElse". Check this out for that...
https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/move-item

Let me know if that helps?
Avatar of oBdA
oBdA

This will expand the files directly to the target folder:
Function Expand-Archive([string]$Path, [string]$Destination) {
	$7z_Application = "C:\Program Files\7-Zip\7z.exe"
	$7z_Arguments = @(
		'x'							## eXtract files with full paths
		'-y'						## assume Yes on all queries
		"`"-o$($Destination)`""		## set Output directory
		"`"$($Path)`""				## <archive_name>
	)
	& $7z_Application $7z_Arguments 
}

Get-ChildItem "C:\source" -Filter *.7z | ForEach-Object {
	Expand-Archive -Path $_.FullName -Destination "\\servername\c$\destination"
}

Open in new window

Avatar of Isaiah Melendez

ASKER

ObDA,

Your solution is 99% there. The only part I am missing was actually moving/deleting the files from the source location. So basically, if test1.7z and test2.7z are in the c:\source dir I want them to get deleted once the extract has been done into the \\servername\c$\destination location.

Does that make sense?
Hey there sj77, use Remove-Item * -Include *.7z or something similar to do that.

https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/remove-item
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

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
Greg,

Like this?

Function Expand-Archive([string]$Path, [string]$Destination) {
	$7z_Application = "C:\Program Files\7-Zip\7z.exe"
	$7z_Arguments = @(
		'x'							## eXtract files with full paths
		'-y'						## assume Yes on all queries
		"`"-o$($Destination)`""		## set Output directory
		"`"$($Path)`""				## <archive_name>
	)
	& $7z_Application $7z_Arguments 
        Remove-Item * -Include *.7z
}

Open in new window

oBdA's response is very much on point and thorough sj77. I want to keep that in my toolbox going forward too :P
Hello guys,

can you please help me how to use the Expand-Archive function if a 7z file is password protected? Of course, i know the password :)

Thanks in advanced for your help.

Best regards,
Zoltan