- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check the website status code using PowerShell?
Website status code is meant to be the status of the website as if the website request from the client is successful or not if the website is available or any error on the webpage which is causing the handshake failed with the client.
There are various website status codes. Please refer to the below link for them.
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
To retrieve the status using PowerShell, we will first connect the webpage using the Invoke-WebRequest command and then we can use the property StatusCode. For example,
$req = Invoke-WebRequest -uri "https://theautomationcode.com" $req
Output
StatusCode : 200 StatusDescription : OK Content : <!DOCTYPE html> <html lang="en-US"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="profile" href="http://gmpg.org/xfn/11"> <script>...
We can see the status code in the output. Here 200 code means the connection is successful and no error on the webpage.
To retrieve only the status code,
$req.StatusCode
In the case of page unavailability, it throws the exception and the status code can not be captured, so we need to use a try/catch mechanism for handling the exception and generating the status code.
try{ $req = Invoke-WebRequest -uri "https://theautomationcode.com/Unknownpage" Write-output "Status Code -- $($req.StatusCode)" } catch{ Write-Output "Status Code --- $($_.Exception.Response.StatusCode.Value__) " }
Output
Status Code --- 404
- Related Articles
- C++ code to check review vote status and uncertainty
- How to get the Accelerated networking status of Azure VM using PowerShell?
- How to get website links using Invoke-WebRequest in PowerShell?
- How to Validate your Website Code?
- How to get the Response status code in Golang?
- How to retrieve the MSI package product code using PowerShell?
- How to get Response Status Code with Selenium WebDriver?
- How to check notifications status for the iOS App
- How to check if the file is empty using PowerShell?
- How to get all the services based on their status in PowerShell?
- How to get website SSL certificate validity dates with PowerShell?
- How to check windows certificate expiry date using PowerShell?
- How to check Azure VM Power state using PowerShell?
- Checking HTTP Status Code in Selenium.
- How to check if remote ports are open using PowerShell?
