PowerShell: Download and Expand Zip File – Legacy Compatible

        function downloadFile{
            param(
                $url,
                $tempFolder="C:\Temp"
            )
            try{
                $fileName=split-path $url -leaf
                $tempFile = "$tempFolder\$fileName"
                try{[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12}catch{}
                New-Item -ItemType Directory -Force -Path $tempFolder
                $webclient = New-Object System.Net.WebClient
                $WebClient.DownloadFile($url,$tempFile)
                return $true
            }catch{
                write-warning $_
                return $false
            }
        }

        function expandZipfile($file,$destination){      
            $destination=if($destinatione){$destination}else{split-path $file -parent}
            try{
                Add-Type -AssemblyName System.IO.Compression.FileSystem
                [System.IO.Compression.ZipFile]::ExtractToDirectory($file, $destination)
            }catch{
                write-warning $_
            }
        }

            $downloaded=downloadFile "https://kimconnect.com/wp-content/uploads/2019/08/emcopy.zip"
            if($downloaded){
                $destinationFolder="C:\Windows\System32"
                expandZipfile $destinationFile -Destination $destinationFolder
            }

Leave a Reply

Your email address will not be published. Required fields are marked *