9

I am running an Invoke-WebRequest in a Windows Powershell as an Administrator.

When I run the following command: Invoke-WebRequest http://speedtest.newark.linode.com/100MB-newark.bin -OutFile $env:TEMP (as recommended here), I get an error stating Access to Path is Denied (see image below).

enter image description here

Things I tried that didn't work:

  • Ran the command on Windows Server 2008 and 2012, as well as, Windows 8.1.
  • Unchecked the Read-Only setting permissions under the Temp folders properties.
  • I changed $env:TEMP to C:\.

The error is consistent across all operating systems tested.

nu everest
  • 957
  • 3
  • 14
  • 27

2 Answers2

13

It looks like you are getting an access denied because the OutFile parameter is trying to create a file named TEMP in AppData/Local folder but there is already a directory named TEMP so there is a naming conflict. I received the same error running your command as is, then I added a file name and it worked. See below:

Invoke-WebRequest http://speedtest.newark.linode.com/100MB-newark.bin -OutFile $env:TEMP\100MB-newark.bin
0

Your command works on my System (Windows-7 SP1 x64). Running as regular User and Administrator both work... (Seems risky as Administrator though). I Tested both x86 and x64 versions of Powershell

Algorithm             Hash                    Path
---------             ----            --------------------
SHA256    A99192624C502AF0BF635D1186AC6ECAD613F0E4A48F5BA8D47B6E261C204908    C:\Temp\scratch\100MB-newark.bin

SHA1    79105A819B8A0FB67DDCDEADC8E47C7F59DB8677    C:\Temp\scratch\100MB-newark.bin

MD5    5F293997D8F256F9C6880272E0773429    C:\Temp\scratch\100MB-newark.bin

Here is the nifty little Get-Webfile function I used: Add to your $PROFILE or . source it. :)

Function Get-Webfile ($url)
{
    $dest=(Join-Path $pwd.Path $url.SubString($url.LastIndexOf('/')))
    Write-Host "Downloading $url`n" -ForegroundColor DarkGreen;
    $uri=New-Object "System.Uri" "$url"
    $request=[System.Net.HttpWebRequest]::Create($uri)
    $request.set_Timeout(5000)
    $response=$request.GetResponse()
    $totalLength=[System.Math]::Floor($response.get_ContentLength()/1024)
    $length=$response.get_ContentLength()
    $responseStream=$response.GetResponseStream()
    $destStream=New-Object -TypeName System.IO.FileStream -ArgumentList $dest, Create
    $buffer=New-Object byte[] 10KB
    $count=$responseStream.Read($buffer,0,$buffer.length)
    $downloadedBytes=$count
    while ($count -gt 0)
        {
        [System.Console]::CursorLeft=0
        [System.Console]::Write("Downloaded {0}K of {1}K ({2}%)", [System.Math]::Floor($downloadedBytes/1024), $totalLength, [System.Math]::Round(($downloadedBytes / $length) * 100,0))
        $destStream.Write($buffer, 0, $count)
        $count=$responseStream.Read($buffer,0,$buffer.length)
        $downloadedBytes+=$count
        }
    Write-Host ""
    Write-Host "`nDownload of `"$dest`" finished." -ForegroundColor DarkGreen;
    $destStream.Flush()
    $destStream.Close()
    $destStream.Dispose()
    $responseStream.Dispose()
}

** Perhaps a Measure-Command could be (more) useful somewhere in that pipeline to provide speed.