Search code examples
powershelldownloadexecute

Powershell Downloading and Executing Files (File or Directory is corrupted and unreadable)


I've been trying to download and execute files with powershell. Everything works fine, Powershell downloads the File from "Workupload.com" and puts it in my /user folder. This works perfectly with pictures, and I can open them as well after they are downloaded.

However it doesnt work when I try it with .exe or .txt files.. Here is my Code:

$WebClient = New-Object System.Net.WebClient
$url = 'link to my .exe on workupload'
$dst = 'FireFox-Installer.exe' #Not adding a Path just puts the File in the User Folder
$WebClient.DownloadFile($url, $dst)
Start-Process $dst 'FireFox-Installer.exe'

Now when I try to open the file I get an Error that the File is corrupted and unreadable. I wonder how I fix that.. Is the Windows-Defender blocking it? Is my Code wrong? Or is it the File-Hoster which causes the errors?

Here is my error-code:

Start-Process : This command cannot be run due to the error: The file or directory is corrupted and unreadable.
At line:1 char:1
+ Start-Process $dst 'FireFox-Installer.exe'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

Would really appreciate some help since im kind of new to Powershell, thank you everyone! :)


Solution

  • Ok so I just tried this and it worked as expected:

    $WebClient = New-Object System.Net.WebClient
    $url = 'https://download-installer.cdn.mozilla.net/pub/firefox/releases/79.0/win32/en-US/Firefox%20Installer.exe'
    $dst = 'C:\Temp'
    $WebClient.DownloadFile($url, (Join-Path $dst 'FireFox-Installer.exe'))
    Start-Process (Join-Path $dst 'FireFox-Installer.exe')
    

    I just added another Join-Path for the destination argument of DownloadFile If it still doesn't work, I would suggest you to check your link and to test your Firefox-Installer.exe file.