3 Methods to Obtain a File in PowerShell


In case you use PowerShell to handle Home windows, in the end you will need to obtain a file. That is very true for Home windows Server, as finest practices have lengthy warned towards shopping the Web from an internet browser put in on the server.

PowerShell is surprisingly versatile on the subject of file downloads. Actually, there are at the least three alternative ways you possibly can obtain a file in PowerShell.

  1. invoke-webrequest
  2. new merchandise
  3. start-bits switch

Earlier than explaining how, I might like to notice that every of the three obtain applied sciences accepts a standard set of parameters, These parameters embody a), the URL that factors to the file being downloaded, and b), a path the place the file shall be saved in your native system after it’s downloaded.

As a finest apply, I like to recommend storing the trail and URL in variables. For the needs of this text, I will be utilizing the variables $URL and $Path. Right here is an instance of how you’d declare these variables nonetheless you need to substitute the values ​​with yours.

$URL = “http://192.168.0.1/MyFile.txt”

$Path=”C:FilesMyFile.txt”

Notice that the trail incorporates not solely the file system location, but additionally the file identify used for the file you need to obtain. This file identify should not match the file identify inside the URL. Actually, you should use a unique file identify as a solution to rename a file whenever you obtain it.

1. Invoke-WebRequest Cmdlet

The Invoke-WebRequest cmdlet is my go-to cmdlet for downloading information. Nonetheless, any of the three cmdlets I present you’ll work.

The syntax utilized by the Invoke-WebRequest cmdlet is straightforward. This is what a obtain command may seem like:

Invoke-WebRequest -URI $URL -OutFile $Path

Notice that the URL is mapped to a parameter referred to as -uri, no -url. In case you attempt to identify the parameter -URL, you’ll obtain an error message as proven in Determine 1.

Brian Posey

Determine 1. You should definitely enter -URI because the parameter identify, not -URL.

In Determine 2, you possibly can see what the obtain course of appears to be like like together with the downloaded file.

Brian PoseyScreenshot shows the Invoke-WebRequest cmdlet used to download a file

Determine 2. You utilize the Invoke-WebRequest cmdlet to obtain the file as follows.

2. New-Object cmdlet

You too can use the New-Object cmdlet to obtain a file in PowerShell.

The New-Object cmdlet, as chances are you’ll already know, can be utilized to create .NET Framework objects or COM objects. In case your purpose is to obtain a file, you might want to create a System.Web.WebClient object. Once you create this object, it’s essential to name the DownloadFile methodology and supply values ​​for the URL and output path.

Right here is an instance of what this command appears to be like like:

(New-Object System.Web.WebClient).DownloadFile ($URL, $Path)

When utilizing this command, ensure that the New-Object cmdlet is in parentheses. Determine 3 exhibits you the command in motion.

Brian PoseyScreenshot showing the New-Object cmdlet used to download a file in PowerShell

Determine 3. This is find out how to use the New-Object cmdlet to obtain a file.

3. Begin-Bits Switch

The third manner you possibly can obtain information in PowerShell is by doing a BITS switch.

BITS stands for Background Clever Switch Service. This service is part of the Home windows working system, and it’s essential to allow BITS for this know-how to work.

The beginning-bitstransfer command is simply as simple to make use of as the opposite two instructions. The command appears to be like like this:

Begin-BitsTransfer -Supply $URL -Vacation spot $Path

Determine 4 presents an instance of the command. Though the determine would not present it, BITS transfers embody an onscreen progress bar that you should use to watch downloads.

Brian PoseyScreenshot showing the BITS transfer used to download the file in PowerShell

Determine 4. That is the way you carry out BITS switch utilizing PowerShell.



Supply hyperlink