PowerShell Script to Get HTTP Headers

Quick script to grab HTTP headers from a given URL.

I named the script Get-HTTPHeaders.ps1, you can save it as whatever you’d like, or, incorporate it into your script as a function, or not even read this post… whatever you want to do. I just find it handy, and wanted to share it.

param(
    [Parameter(ValueFromPipeline=$true)]
    [string] $Url
)

$request = [System.Net.WebRequest]::Create( $Url )
$headers = $request.GetResponse().Headers
$headers.AllKeys |
     Select-Object @{ Name = "Key"; Expression = { $_ }},
     @{ Name = "Value"; Expression = { $headers.GetValues( $_ ) } }

All you have to do is pass it a URL as a parameter. See the example below:

image

Advertisement
%d bloggers like this: