PowerShell Download File and Extract File

|

Grabbing code from Powershell Download Script and Powershell Extract Zip Files this script will download with the option of extracting the zip file.

In PowerShell if you type the following you will get two examples of how to use the code.

get-help DownloadFileExtractFile.ps1 -examples
<#
.SYNOPSIS
    Downloads one file from a defined URL to a defined directory path with the option of extracting to a defined path
.DESCRIPTION
    This script is designed to take a defined URL and download it to a defined directory on your computer.
    The script will assume the file name part of the URL is the file name for the computer.
.PARAMETER Path
.PARAMETER LiteralPath
.Example
    DownloadFileExtractFile.ps1 http://www.kittell.net/downloadfile.txt c:usersdkittelldesktopdownload
    Only Download
.Example
    DownloadFileExtractFile.ps1 http://www.kittell.net/downloadfile.txt c:usersdkittelldesktopdownload 1 c:usersdkittelldownloadextracted
    Download and Extract
.Inputs
    [String]$WebURL
    [String]$FileDirectory
.Link
   
PowerShell Download File and Extract File
#> # URL Parameter $WebURL = $args[0] # Directory Parameter $FileDirectory = $args[1] # Extract Parameter # If you want to extract the downloaded file pass a 1 followed by the extract folder path $DoExtract = $args[2] # Extract Folder $ExtractFileDirectory = $args[3] # If directory doesn't exist create the directory if((Test-Path $FileDirectory) -eq 0) { mkdir $FileDirectory; cd $FileDirectory; } # We assume the file you download is named what you want it to be on your computer $FileName = [System.IO.Path]::GetFileName($WebURL) # Concatenate the two values to prepare the download $FullFilePath = "$($FileDirectory)$($FileName)" # Give a basic message to the user to let them know what we are doing Write-Host "Downloading '$WebURL' to '$FullFilePath'" $uri = New-Object "System.Uri" "$WebURL" $request = [System.Net.HttpWebRequest]::Create($uri) $request.set_Timeout(15000) #15 second timeout $response = $request.GetResponse() $totalLength = [System.Math]::Floor($response.get_ContentLength()/1024) $responseStream = $response.GetResponseStream() $targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $FullFilePath, Create $buffer = new-object byte[] 10KB $count = $responseStream.Read($buffer,0,$buffer.length) $downloadedBytes = $count while ($count -gt 0) { [System.Console]::Write("`r`nDownloaded {0}K of {1}K", [System.Math]::Floor($downloadedBytes/1024), $totalLength) $targetStream.Write($buffer, 0, $count) $count = $responseStream.Read($buffer,0,$buffer.length) $downloadedBytes = $downloadedBytes + $count } $targetStream.Flush() $targetStream.Close() $targetStream.Dispose() $responseStream.Dispose() # Give a basic message to the user to let them know we are done Write-Host "`r`nDownload complete" if($DoExtract -eq 1) { Write-Host "`r`nExtracting Download" $Shell = new-object -com shell.application # If directory doesn't exist create the directory if((Test-Path $ExtractFileDirectory) -eq 0) { mkdir $ExtractFileDirectory; cd $ExtractFileDirectory; } # Get the name of the Zip file $Zip = $Shell.NameSpace($FullFilePath) #Expand/Extract each file from the zip file foreach($Item in $Zip.items()) { $Shell.Namespace($ExtractFileDirectory).copyhere($Item) } Write-Host "`r`nDownload Extracted" }
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.