How To Download Files From PowerShell In Windows 10
PowerShell became a default app in Windows with Windows 8. It’s not a replacement for Command Prompt but it can do things that Command Prompt can’t, and more often it’s easier to use. A common example of this is downloading files. If you want to download a file from the Command Prompt, you’ve got quite a few hoops that you’ll need to jump through. Normally, you’ll need to install an app like wget or Cygwin. With PowerShell, it’s much simpler. You can download files from PowerShell and save them to the current folder, or to any other folder of your choice.
Download Files From PowerShell
You don’t need admin rights to download files from PowerShell. All you need to know is the correct command. The following command will download a file and save it to the current directory. If you run PowerShell as a regular user, it opens to your user folder by default. If you do not change the folder, you can find the downloaded file at the root of your user folder.
Syntax:
powershell -command "& { iwr url/FileName.Extension -OutFile FileName.Extension }"
Example:
powershell -command "& { iwr https://addictivetips.com/app/uploads/2018/03/Lines-In-The-Sky.jpg -OutFile image.jpg }"
Save To Folder Location
There are two ways to download files from PowerShell and save them to a folder location. The first method has you switch to the folder you want to save to with the cd command.
For example, when you open PowerShell, it opens your current user folder. You can use the cd command to switch to, for example, the Downloads folder.
Syntax:
cd Folder path
Example:
cd C:\Users\fatiw\Downloads
Once you’re in the new folder, you can use the command from the previous section and download your file.
The second method doesn’t require changing to a different folder location. Instead, you can just add the save location to the command that you execute which is as follows.
Syntax:
$client = new-object System.Net.WebClient $client.DownloadFile("url/FileName.Extension","SaveLocation\FileName.Extension")
Example:
$client = new-object System.Net.WebClient $client.DownloadFile("https://addictivetips.com/app/uploads/2018/03/Lines-In-The-Sky.jpg","C:\New folder\image1.jpg")
Caveats
PowerShell will not check if the file name you’ve given is already being used. It will overwrite a file without warning so if you already have a file named image.jpg in a folder, make sure you don’t download and save another file with the same name.
If you’re running PowerShell as a regular user, it will not let you save files to the root of your Windows drive.
PowerShell will not correct file extensions. If you use PowerShell to download a JPG file and tell it to save it as a PNG when giving it the save location, PowerShell will not correct you. This will result in you being unable to open the file because the extension is incorrect. You will have to fix the file extension before you can open the file.
Thank you for this. I’ve been looking around for how to do this for a while now.