How to Download File in PowerShell

Migel Hewage Nimesha Feb 02, 2024
How to Download File in PowerShell

Users can download CSV files from Websites where there are important data, usually large files with statistical information. For example, the publicly available link below would let you download the CSV file of the Annual Enterprise Survey - 2021 by the Government of New Zealand. We will use the following CSV file URL in this article to demonstrate how to do a similar function through Windows PowerShell commands.

https://www.stats.govt.nz/assets/Uploads/Annual-enterprise-survey/Annual-enterprise-survey-2020-financial-year-provisional/Download-data/annual-enterprise-survey-2020-financial-year-provisional-csv.csv

Windows PowerShell is a Scripting solution, running on Command Line Instructions to provide flexibility similar to Unix-like command-line-based systems to Windows users. Additionally, a version of PowerShell is available for Mac users (Use of Commands would be slightly different from Windows).

In a normal web browser, the URL directly downloads the CSV file to the downloads folder specified by the user, without any issues. When you try to download the same CSV in PowerShell, certain commands are required.

Download a File Through Windows PowerShell

The basic command that can be used to download a file through Windows PowerShell is Invoke-WebRequest.

Through this command, a web request PowerShell initiates a web request. Then the required URL is provided.

The important thing to note here is the need for an output file to save the data retrieved from the web request. You should first create a file type of the type of the file you intend to download data from. As our requirement, we create a .csv type file to save the data passed through the web request from the above URL.

It is necessary to keep in mind that not only CSV, but other file types can also be downloaded in this manner.

Using Invoke-WebRequest to Download a CSV File in PowerShell

Then the Invoke-WebRequest can be used in the following manner.

Invoke-WebRequest URL -OutFile File_path

Here, -OutFile would be the file to save data from online CSV file.

Below shows the complete execution with our sample URL.

Invoke-WebRequest https://www.stats.govt.nz/assets/Uploads/Annual-enterprise-survey/Annual-enterprise-survey-2020-financial-year-provisional/Download-data/annual-enterprise-survey-2020-financial-year-provisional-csv.csv -OutFile .\SaveCSV.csv

As the output in the PowerShell you would see the below script running till action completes.

Writing web request                                                                                                
Writing request stream... (Number of bytes written: 33451654)

The output of the above execution is similar to the file you would directly download.

However, there are constraints to using this Invoke-WebRequest. When the website requires simple password-based authentication, it is required to include your login details and the Invoke-WebRequest.

Invoke-WebRequest URL -Add your lgin information- -SessionVariable MyNewSession

You will see the below script running when you run the above stream until the download completes.

Writing web request                                                                                                Writing request stream... (Number of bytes written: 1474178)

Then it would create a web session for you and download the required file. It would be in .cs format, which you can change to .csv format to make it usable with any .csv readable application.

But if your website requires two-factor authentication, or any other multi-factor authentication systems, initiating the web session as indicated here would be troublesome. Therefore, it is advised to use a personal link (Personal cloud link - private access) to download the file through PowerShell.

Therefore, you can easily download the CSV or other file types through PowerShell through these above commands. As explained, it would be either to an output file you provided, which would be easy to use, given that the correct format is considered, or download the file and convert it to a readable format.

Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.