Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Download File With Credentials Using PowerShell

July 19, 2024
in A, Blog, Powershell
0
ADVERTISEMENT

Table of Contents

How to Download File With Credentials Using PowerShell

In some cases, you want to download a file from a direct link using PowerShell script. You can use the below code to download them from a public environment normally:

$source = 'http://downloads.bonguides.com/10MB.zip'
$destination = 'D:\Donwloads\10MB.zip'
Invoke-WebRequest -Uri $source -OutFile $destination

But if the source requires authentication before allowing access? For example, the code below downloads a file from a private website where users must log in. Whether the source location requires users to log in, you would get the error 401 Authorization Required

yAiBkqMybSRgZdBooZWXBIXDXxznYaslLClj0EM1T9aiCAHXxkdy5xvAG1gQ

If authentication is required, you should add a credential to the request using the –Credential parameter. The first line in the code below prompts you to enter the credential (username and password) and stores it to the $credential variable.

$credential = Get-Credential
$source = 'http://downloads.bonguides.com/10MB.zip'
$destination = 'D:\Downloads\10MB.zip'
Invoke-WebRequest -Uri $source -OutFile $destination -Credential $credential

The demonstration below shows what you’d expect to see when you run the above code in PowerShell. As you can see, the Get-Credential cmdlet prompted a PowerShell credential request. This time, using the credential with Invoke-WebRequest resulted in a successful download.

K6CJgyIgZ3QZOMABj62bQD1rCQtosDhIwhvEjJQbv8ySn1tymADErWKeAktC

After authenticated to the source, the file should be downloaded automatically.

6XQ5E8EncG1fSYtdA4BzewgUj98hzzAVsGZJNyCcYTPfjG97xeyDAUVZ4exe

If you don’t want to enter the credentials manually. You can pre-declare them into the script as the variables.

# URL and Destination
$source = 'http://downloads.bonguides.com/10MB.zip'
$destination = 'D:\10MB.zip'

# Define username and password
$username = 'test'
$password = '123'

# Convert to SecureString
$secPassword = ConvertTo-SecureString $password -AsPlainText -Force

# Create Credential Object
$credObject = New-Object System.Management.Automation.PSCredential ($username, $secPassword)

# Download file
Invoke-WebRequest -Uri $source -OutFile $destination -Credential $credObject

This time, when you run the script, the file should be downloaded automatically without any prompting.

uOtpYhZzuChQCiY1OOsGSyCGzp37LWR8IX4oe6vKZHqYgXg2JbxC5JoRRYCe

Other ways to download a file using PowerShell

Depending on the way you’re using to download a file with PowerShell. The code snippet could be different.

Using the Invoke-RestMethod:

$cred = Get-Credential
$source = 'http://speedtest.tele2.net/10MB.zip'
$dest = 'D:\10MB.zip'
Invoke-RestMethod -Uri $source -OutFile $dest -Credential $cred -AllowUnencryptedAuthentication

Using the WebClient class:

# URL and Destination
$url = 'http://downloads.bonguides.com/10MB.zip'
$dest = 'D:\Downloads\10MB-005.zip'

# Define username and password
$username = 'test'
$password = '123'

# Convert to SecureString
$secPassword = ConvertTo-SecureString $password -AsPlainText -Force

# Create Credential Object
$credObject = New-Object System.Management.Automation.PSCredential ($username, $secPassword)

# Download file
Invoke-WebRequest -Uri $url -OutFile $dest -Credential $credObject

Using the HttpClient class:

# Set the source and destination
$source = 'http://speedtest.tele2.net/10MB.zip'
$destination = 'D:\temp\10MB.zip'

# Load the required assembly
Add-Type -AssemblyName System.Net.Http
 
# Prompt for credentials
$credentials = Get-Credential

# Create the HTTP client download request with credentials
$handler = New-Object System.Net.Http.HttpClientHandler
$handler.Credentials = $credentials
$httpClient = New-Object System.Net.Http.HttpClient($handler)
$response = $httpClient.GetAsync($source)
$response.Wait()
 
# Create a file stream to pointed to the output file destination
$outputFileStream = [System.IO.FileStream]::new($destination, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write)
 
# Stream the download to the destination file stream
$downloadTask = $response.Result.Content.CopyToAsync($outputFileStream)
$downloadTask.Wait()
 
# Close the file stream
$outputFileStream.Close()
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

Fix HTTP Error 404 Not Found Trying Other Mirror CentOS 7

Next Post

How to Wipe a WSL Distro in Windows Subsystem for Linux

Related Posts

Images Hidden Due To Mature Content Settings In CivitAI

August 31, 2024

Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

August 20, 2024

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024

How to Remove The Test Mode Watermark Without Disabling Test Mode

July 28, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory