I want to extract a zip file in Downloads folder to Desktop using PowerShell and C#.
I need it to work with Windows 7, 8, & 10.
I'm trying to take these PowerShell commands
- extract https://stackoverflow.com/a/36472063/6806643
- overwrite https://stackoverflow.com/a/5711383/6806643
- chain https://superuser.com/a/612413/740888
$shell = New-Object -ComObject shell.application
$zip = $shell.NameSpace("zip file path")
foreach ($item in $zip.items()) {
$shell.Namespace("unzip destination path").CopyHere($item)
}
And run it through a C# Process.Start()
Process.Start("powershell.exe",
"timeout 3; "
+ "$shell = New-Object -ComObject shell.application; "
+ "$zip = $shell.NameSpace(\"C:\\Users\\Matt\\Downloads\\MyFile.zip\"); "
+ "foreach ($item in $zip.items()) {$shell.Namespace(\"C:\\Users\\Matt\\Desktop\\\").CopyHere($item, 0x14)}"
);
Problem
PowerShell launches but fails to extract, and closes out before I can read the error.
However, if I copy paste those chained commands into PowerShell without C#, it works.
$shell = New-Object -ComObject shell.application; $zip = $shell.NameSpace('C:\Users\Matt\Downloads\MyFile.zip'); foreach ($item in $zip.items()) {$shell.Namespace('C:\Users\Matt\Desktop\').CopyHere($item, 0x14)}
This works but it's for PowerShell 5 only.
Process.Start("powershell.exe",
"timeout 3; Expand-Archive 'C:\\Users\\Matt\\Downloads\\MyFile.zip' -Force -DestinationPath 'C:\\Users\\Matt\\Desktop\\'"
);