Skip to content

Instantly share code, notes, and snippets.

@mujahidk
Last active May 19, 2023 20:48
Show Gist options
  • Save mujahidk/e22d7c27049235ab1d023577d61d3a4b to your computer and use it in GitHub Desktop.
Save mujahidk/e22d7c27049235ab1d023577d61d3a4b to your computer and use it in GitHub Desktop.
PowerShell WebClient script with basic authentication to export content from a website. Simple alternative to curl in Windows.
# Uncomment to disable Certificate errors
# [Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$url = "http://example.com/data.json"
$user = "data-user"
$password = "password"
# Output file path into current directory.
$file= ($pwd).path + "\export.json"
Write-Host "Exporting data to: $file"
# Create a WebClient
$webclient = New-Object System.Net.WebClient
# Basic authentication encoding.
$basic = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($user + ":" + $password));
# Set Authorization HTTP header with Basic authentication information.
$webclient.Headers["Authorization"] = "Basic $basic"
# Export the contents to file.
$webclient.DownloadFile($url, $file)
REM Run the curl.ps1 in powershell. '-executionpolicy bypass' to run it on systems where running scripts is disabled.
powershell -executionpolicy bypass -file curl.ps1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment