PowerShell – Download and Execute File

email me

Download a file from the web and launch it

$ProcName = "NoSleep.exe"
$WebFile = "https://eddiejackson.net/apps/$ProcName"


(New-Object System.Net.WebClient).DownloadFile($WebFile,"$env:APPDATA\$ProcName")
Start-Process ("$env:APPDATA\$ProcName")


Google Link

Something pretty cool, you could use a Google Drive File ID in the script above, with some minor editing (the below ID works):


GDrive Example

(New-Object System.Net.WebClient).DownloadFile("https://drive.google.com/uc?export=download&id=0B1ZMU4Jk29FPUUpta0drVW05WnM","$env:APPDATA\test.jpg")
Start-Process ("$env:APPDATA\test.jpg")

…running the above, will download and launch the test.jpg image. This means, you could store all your files on a Google Drive, and have users run a “lite” script, which would then download all the necessary resource files from your Google Drive. All permissions and access can be controlled from the Google Drive.

Single liner

powershell.exe -command PowerShell -ExecutionPolicy bypass -noprofile -windowstyle hidden -command (New-Object System.Net.WebClient).DownloadFile('https://eddiejackson.net/apps/NoSleep.exe',"$env:APPDATA\$ProcName");Start-Process ("$env:APPDATA\NoSleep.exe")

Get around the Google Drive Anti-Virus Message


$FileName = 'foo.exe'
$FileID = "248ADma5BL7mHsLpsV3ciewy13rlCA8X_"

# set protocol to tls version 1.2
# "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12"

$FolderName = "C:\Download\"
if (Test-Path $FolderName) {

Write-Host "Folder Exists"

New-Item $FolderName -ItemType Directory


# Download the virus warning as temp.txt
Invoke-WebRequest -Uri "https://drive.google.com/uc?export=download&id=$FileID" -OutFile "C:\Download\temp.txt"

# Load temp.txt as string
$InputString = Get-Content C:\Download\temp.txt

# Return UUID from string
$match = Select-String "uuid=(.*)" -inputobject $InputString
$retMatch = $match.matches.groups[1].value
$Confirmation_UUID = $retMatch.Substring(0, $retMatch.IndexOf('"'))

# Download the real file
Invoke-WebRequest -Uri "https://drive.google.com/uc?export=download&id=$FileID&confirm=t&$Confirmation_UUID" -OutFile "C:\Download\$FileName"

# Clear Session
$Confirmation_UUID = ""
$InputString = ""
$FileName  = ""
$FolderName = ""
$FileID = ""
Remove-Item "C:\Download\temp.txt"

Return all Methods from Net.WebClient

$objWWW = New-Object Net.WebClient
$objWWW | Get-Member

Alternative Download
wget “https://eddiejackson.net/apps/NoSleep.exe” -outfile “NoSleep.exe”

WebClient Class

tags: MrNetTek