powershelldownloadinvoke-webrequest

PowerShell, download a file if the filename has changed?


I've been reading the answers here on downloading files under certain conditions, and everything there is clear, but I have a bit of a problem with the place I am trying to download from.

I would like to download from the button beside .zip in the bottom left that reads 64 bit (i.e. this would be the 64-bit .zip portable version of the app). https://code.visualstudio.com/download# . I would like to:

The problems are that a) I can't see the filename, and b) if I right-click the link and select Copy Link, that is not a downloadable object.

I am trying to do this in PowerShell.


Solution

  • Note: Both Invoke-WebRequest and the underlying .NET APIs have changed between Windows PowerShell (versions up to v5.1) and PowerShell (Core) 7+, so two distinct solutions are presented below:

    Windows PowerShell solution:

    $hd = Invoke-WebRequest -UseBasicParsing -Method Head 'https://code.visualstudio.com/sha/download?build=stable&os=win32-x64-archive'
    
    # Extract the file name from the response.
    $downloadFileName = $hd.BaseResponse.ResponseUri.Segments[-1]
    

    PowerShell (Core) 7+ solution:

    $hd = Invoke-WebRequest -Method Head 'https://code.visualstudio.com/sha/download?build=stable&os=win32-x64-archive'
    
    # Extract the file name from the response.
    $downloadFileName = $hd.BaseResponse.RequestMessage.RequestUri.Segments[-1]