Powershell – Get HTTP(S) Status Code

(Invoke-WebRequest -Uri https://www.cisco.com -Method Get).StatusCode

This command sends a GET request to the specified URL and then returns the HTTP status code of the response.

Here’s a breakdown of the command:

• `InvokeWebRequest`: This is a PowerShell cmdlet that sends an HTTP(S) request to a web page or web service.
• `Uri https://www.cisco.com`: This parameter specifies the Uniform Resource Identifier (URI) of the Internet resource to which the web request is sent. In this case, it is set to “https://www.cisco.com“.
• `Method Get`: This parameter specifies the method used for the web request. In this case, it is set to “Get“, which is the most common HTTP method. It requests data from a specified resource.
• `StatusCode`: This is a property of the response object returned by `InvokeWebRequest`. It contains the status code of the HTTP response. Common status codes include 200 (OK), 404 (Not Found), and 500 (Internal Server Error).

So the entire command `InvokeWebRequest Uri https://www.cisco.com Method Get).StatusCode` sends a GET request to “https://www.cisco.com” and outputs the status code of the response. If the command is successful, it will return 200. If the URL doesn’t exist, it will return 404. If there’s a server error, it may return 500 or another 5xx status code.

It’s worth noting that the command will throw an error if it fails to reach the website (e.g., if the URL is invalid or the network connection is down), because it doesn’t have `ErrorAction SilentlyContinue` to suppress errors.