Categories: PowerShell

PowerShell – How to check response code from a website

There are times when you need to verify websites availability to be able to tell if it's up or not. Below you can find a simple approach to show whether the site is up or not. This comes useful in situations where website availability is crucial for your business.

🔒 Solution

[string] $url = 'https://evotec.xyz'

function Get-WebStatus($url) {
    try {
        [net.httpWebRequest] $req = [net.webRequest]::create($url)
        $req.Method = "HEAD"
        [net.httpWebResponse] $res = $req.getResponse()

        if ($res.StatusCode -eq "200") {
            write-host "`nSite $url is up (Return code: $($res.StatusCode) - $([int] $res.StatusCode))`n" -ForegroundColor green
        } else {
            write-host "`nSite $url is down`n" ` -ForegroundColor red
        }
    } catch {

        write-host "`nThings went bad (dns issue?). Try again.`n" ` -ForegroundColor red
    }
}

Get-WebStatus $url

To be able to check for the status of a website quickly and efficiently one can use the following code. This code verifies whether site returns Status Code 200. In case of different code returns it's assumed the website is down.

This post was last modified on 6 czerwca, 2025 20:33

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Supercharging Your Network Diagnostics with Globalping for NET

Ever wondered how to run network diagnostics like Ping, Traceroute, or DNS queries from probes…

2 tygodnie ago

Automating Network Diagnostics with Globalping PowerShell Module

Are you tired of manually running network diagnostics like Ping, Traceroute, or DNS queries? The…

2 tygodnie ago

Enhanced Dashboards with PSWriteHTML – Introducing InfoCards and Density Options

Discover new features in the PSWriteHTML PowerShell module – including New-HTMLInfoCard, improved layout controls with…

3 tygodnie ago

Mastering Active Directory Hygiene: Automating SIDHistory Cleanup with CleanupMonster

Security Identifier (SID) History is a useful mechanism in Active Directory (AD) migrations. It allows…

3 tygodnie ago

Upgrade Azure Active Directory Connect fails with unexpected error

Today, I made the decision to upgrade my test environment and update the version of…

3 tygodnie ago

Mastering Active Directory Hygiene: Automating Stale Computer Cleanup with CleanupMonster

Have you ever looked at your Active Directory and wondered, "Why do I still have…

3 tygodnie ago