Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PowerShell Download File and Extract File

Posted on September 17, 2015 By David Kittell

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.

Related

Code PowerShell

Post navigation

Previous post
Next post

Related Posts

Read Text File Line By Line

Posted on September 18, 2013October 26, 2015

Public Class ReadFromFile Shared Sub Main() Dim sFileName As String Dim srFileReader As System.IO.StreamReader Dim sInputLine As String sFileName = "test.txt" srFileReader = System.IO.File.OpenText(sFileName) sInputLine = srFileReader.ReadLine() Do Until sInputLine is Nothing System.Console.WriteLine(sInputLine) sInputLine = srFileReader.ReadLine() Loop End Sub End Class Originally Posted on September 18, 2013Last Updated on October…

Read More

Azure CLI – Download All Blobs

Posted on September 15, 2017

If you ever need to get your files out of Azure this script will help you get your files out of Blob Storage azBlob="<blob name>" azContainer="<container name>" azStorageKey="<primary or secondary storage key>" fpDestination="$HOME/Desktop/$azBlob/$azContainer" # Change this to a path you want mkdir -p $fpDestination echo "Listing the blobs from $sContainer…"…

Read More

Ubuntu – PPTPD VPN Install & Configure

Posted on December 28, 2015December 28, 2015

# Install PPTPD VPN & UFW Firewall sudo apt-get install pptpd ufw # Allow SSH sudo ufw allow 22 # Allow PPTP VPN sudo ufw allow 1723 # Allow HTTP – Only if you need it (If you don’t run a website from the box don’t add this) sudo ufw…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes