In this article, we’ll take a look at how to use native PowerShell cmdlets to download a file via HTTP, HTTPS, or FTP protocol. You can use PowerShell cmdlets as a native alternative of the wget and curl tool on Windows 10 and Windows Server 2016.
First, check the PowerShell version installed on your computer:
$psversiontable
For versions of PowerShell earlier than 3.0, the System.Net.WebClient class must be used to download a file from the Internet. For example, on Windows 7/Windows Server 2008 R2 (on which PowerShell 2.0 is installed by default), you can use the following PowerShell commands to download a file from the HTTP(S) website and save it to a local drive:
$download_url = "https://theitbros.com/wp-content/uploads/2018/08/cropped-logo_fon-1-2.png" $local_path = "C:\PS\theitbros_logo.png" $WebClient = New-Object System.Net.WebClient $WebClient.DownloadFile($download_url, $local_path)
To download a file from an FTP server with authorization, you need to specify the FTP username and password in the script:
$download_url = "ftp://hpe.com/iso/psp9.2.iso" $local_path = "C:\Downloads\psp9.2.iso " $user = "hpeFtpUserName" $pass = "Str0ng3$tPa$$" $WebClient = New-Object System.Net.WebClient $WebClient.Credentials = New-Object System.Net.NetworkCredential($user, $pass) $WebClient.DownloadFile($download_url, $local_path)
On Windows 10, you can use the built-in Invoke-WebRequest cmdlet to download files (this cmdlet is available in all versions since PowerShell 3.0). To download a file, you just need to specify its URL and the local folder in which to save the file:
Invoke-WebRequest https://theitbros.com/wp-content/uploads/2018/08/cropped-logo_fon-1-2.png -OutFile C:\ps\logo.png
Hint. By default, the Invoke-WebRequest cmdlet downloads the file to the current directory. If you need to change the directory and/or file name, use the –OutFile parameter.
On Windows 10, there are two aliases available for the Invoke-WebRequest cmdlet: curl and wget.
So, to download a file from the Internet website, you can use a shorter command. Instead of typing a full cmdlet name, you can use (for example):
wget https://theitbros.com/backup_file.zip -OutFile C:\downloads\backup.zip
Hint. The Invoke-WebRequest doesn’t perform a check if the target file exists. If the file already exists, it is overwritten without any warning.
If you need to authenticate under the current user on a remote website (via NTLM or Kerberos), you need to add the –UseDefaultCredentials parameter:
Curl 'http://theitbros.local/file.zip' -UseDefaultCredentials
Or you can specify credentials manually:
Credentials = Get-Credential Invoke-WebRequest -Uri https://theitbros.com/backup_file.zip -OutFile C:\downloads\backup.zip -Credential $Credentials
You can additionally pass some information in the HTTP header request. For example, set the API key:
Invoke-WebRequest -H @{'apiKey'='keyValue'} https://theitbros.com/backup_file.zip -OutFile C:\ps\file.zip
If you need to download a list of files in a batch, save their URLs to a plain txt file and use the following command to start the download:
Get-Content urls.txt | ForEach-Object {Invoke-WebRequest $_ -OutFile $(Split-Path $_ -Leaf)}
- Lens Kubernetes IDE – Opensource Lens Desktop - January 27, 2023
- Using Select-Object Cmdlet in PowerShell - January 26, 2023
- How to Turn Off Siri Suggestions on iPhone? - January 25, 2023
WebRequest gives memory errors with large files so is unusable.