Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell download and run exe file

Tags:

powershell

i need to download file from website and then run this file. File is in exe format. I tried many commands, but unsuccessfully. Could you help me. Thanks a lot for help and have a nice day.

like image 453
YouYyn Avatar asked Nov 04 '17 12:11

YouYyn


People also ask

How do I download and run a file in PowerShell?

We are going to start with the most common way to download a file from an URL with PowerShell. For this, we will be using the Invoke-WebRequest cmdlet. To download a file we need to know the source URL and give up a destination for the file that we want to download. The parameter -OutFile is required.


1 Answers

The script you are wanting is going to do two things. First, we will download a file and store it in an accessible location. Second, we will then run the executable with whatever arguments we need to have it install successfully.

Step 1:

We have two ways of accomplishing this task. The first is to use Invoke-Webrequest. The only two arguments we need for it will be the URL of the .exe file, and where we want that file to go on our local machine.

$url = "http://www.contoso.com/pathtoexe.exe"
$outpath = "$PSScriptRoot/myexe.exe"
Invoke-WebRequest -Uri $url -OutFile $outpath

I use $PSScriptRoot here because it will let me drop the exe right next to where the Powershell script is running, but feel free to put a path of your choice in, like C:/temp or downloads or whatever you want. You may notice that with larger files, the Invoke-WebRequestmethod takes a long time. If this is the case, we can call .Net directly and hopefully speed things up.

We will set our variables of $url and $outpath the same, but instead of Invoke-WebRequest we will use the following .Net code:

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $outpath)

Step 2:

Calling an executable is the easy part.

$args = @("Comma","Separated","Arguments")
Start-Process -Filepath "$PSScriptRoot/myexe.exe" -ArgumentList $args

And that should just about do it for you.

like image 107
Bryce McDonald Avatar answered Sep 25 '22 07:09

Bryce McDonald