Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@barryokane
Last active October 28, 2022 10:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save barryokane/275eb5006b3631a732935477bdff71b0 to your computer and use it in GitHub Desktop.
Save barryokane/275eb5006b3631a732935477bdff71b0 to your computer and use it in GitHub Desktop.
Powershell script to loop over a list of URLs, make a HTTP HEAD request and check for (first) 301 response.
#-------------
# Script to loop over a list of URLs, make a HTTP HEAD request and check for (first) 301 response
# INPUT: A txt file with one URL per line
#
# OUTPUT: A CSV file with columns for:
# - RequestURI = the URI from than line in input file
# - StatusCode = response status code (blank if error code!)
# - Error = Error message (for 404 or 500 errors)
#
$linksFilePath = "C:\Temp_Stuff\{your input file}.txt"
$outCSVPath = "C:\Temp_Stuff\{your output file}.csv"
get-content $linksFilePath |
Foreach { $uri = $_; try { Invoke-WebRequest -Uri $uri -Method HEAD -MaximumRedirection 0 -ErrorAction SilentlyContinue -UseBasicParsing } catch {
New-Object -TypeName psobject -Property @{ Error = $_ } } } |
Select @{Name="RequestURI";Expression={$uri}}, StatusCode, @{Name="RedirectTo";Expression={$_.Headers["Location"]}}, Error |
Export-Csv $outCSVPath
@shivagra
Copy link

Hi,
I want to check redirects created for multilingual website, the URL contains "locale" keyword, so I need to replace it with language and then check where it is redirected to. Any help will be much appreciated. Thanks

@barryokane
Copy link
Author

Hi,
I want to check redirects created for multilingual website, the URL contains "locale" keyword, so I need to replace it with language and then check where it is redirected to. Any help will be much appreciated. Thanks

@shivagra : this little powershell script loops over a list of URLs and lists any redirects. So, maybe you could create and "input file" with all your locale Urls (one per line) and use this script?

@tancerk
Copy link

tancerk commented Oct 28, 2022

Tnx for this.Its saved my day , when i need checking for nodes with forbiden access mesages.
By Karel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment