How to overwrite unzip

I am having a script for unziping file and add parameter -Force but i have error that file exist

Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
    param([string]$zipfile, [string]$outpath)

    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
[string]$zipFN_1 = 'D:\FolderWhereIsZip\'
[string]$name = "CHANNEL" + "_" + (get-date -UFormat “%Y_%m_%d”)
[string]$zipFN = $zipFN_1 + $name + '.zip'
Unzip "$zipFN" "D:\MYFolder4Unzip\" -Force

Well you’re specifying the force parameter but you’re not doing anything with it…

You could do something like this,

Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
    param([string]$zipfile, [string]$outpath, [switch]$force)

    if ($force) {$overWrite = $true}

    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath, $overwrite)    
}

[string]$zipFN_1 = 'D:\FolderWhereIsZip\'
[string]$name = "CHANNEL" + "_" + (get-date -UFormat “%Y_%m_%d”)
[string]$zipFN = $zipFN_1 + $name + '.zip'
Unzip "$zipFN" "D:\MYFolder4Unzip\" -Force

But I got an error when I tested; apparently the boolean to specify the overwrite is only supported in PowerShell .NET Core. That would fit with my testing as it failed in 5.1 but works in 7.2.

I think you’ll have to do this manually so if Force is specified, use Test-Path and Remove-Item to clean up before extracting.

Also, are you running an early version of PowerShell? You can unzip natively now with Expand-Archive. None of that .NET nonsense :smiley: