Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / Running Simple HTTP Web Server Using PowerShell

April 14, 2023 PowerShellWindows 10Windows Server 2019

Running Simple HTTP Web Server Using PowerShell

For testing purposes or as a simple stub at the service deployment stage, I regularly need to run a simple web server on Windows. To avoid a full-featured IIS installation, you can run a simple HTTP web server directly from your PowerShell console. You can run such a web server on any TCP port using the built-in System.Net.HttpListener .NET class.

Open your PowerShell console and create an HTTP listener:

$httpListener = New-Object System.Net.HttpListener

Then specify the port to listen. In this example, I want to run an HTTP web server on Port 9090.

$httpListener.Prefixes.Add("http://localhost:9090/")

  • Make sure that this port is not being used by other processes. You can find out which process is listening on a TCP or UDP on Windows.
  • In order to listen in all network interfaces, use the http://+:9090/ address.

Start the listener:

$httpListener.Start()

You can use different authentication types (Basic, Digest, Windows, Negotiate, or NTLM) in your HttpListener object or bind an SSL certificate to implement HTTPS.

If you run this code, a separate process waiting for connection on port 9090 appears in Windows. Check it using the command:

nestat –na 9090

or display a list of open ports using PowerShell:

Get-NetTCPConnection -State Listen | Select-Object -Property LocalAddress, LocalPort, State | Sort-Object LocalPort |ft

powershell: run http listener

Remember to open this port in Windows Defender Firewall. You can quickly create a rule to open a firewall port by using the PowerShell command:

New-NetFirewallRule -DisplayName "AllowTestWebServer" -Direction Inbound -Protocol TCP –LocalPort 9090 -Action Allow

Now create a text file on your hard drive with the HTML you want your webserver to display. For example:

<!DOCTYPE html>
<html>
<head>
<title>
Lightweight PowerShell Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {background-color:#ffffff;background-repeat:no-repeat;background-position:top left;background-attachment:fixed;}
h1{font-family:Arial, sans-serif;color:#000000;background-color:#ffffff;}
p {font-family:Georgia, serif;font-size:14px;font-style:normal;font-weight:normal;color:#000000;background-color:#ffffff;}
</style>
</head>
<body>
<h1>Test web page </h1>
<p>This web page was generated by PowerShell using the System.Net.HttpListener class</p>
</body>
</html>

HTML file for display on HTTP web server with PowerShell

I saved this HTML code to C:\PS\testwebpage.html with UTM-8 encoding.

Then run the following commands to read your HTML file and send a response to a user’s browser.

$context = $httpListener.GetContext()
$context.Response.StatusCode = 200
$context.Response.ContentType = 'text/HTML'
$WebContent = Get-Content  -Path "C:\PS\testwebpage.html" -Encoding UTF8
$EncodingWebContent = [Text.Encoding]::UTF8.GetBytes($WebContent)
$context.Response.OutputStream.Write($EncodingWebContent , 0, $EncodingWebContent.Length)
$context.Response.Close()

Run simple Web Server using PowerShell

Open the URL address of your HTTP server in a browser (http://localhost:9090) or use PowerShell to get the content of a web page. The script returns an HTML code only once, after which your listener will be stopped automatically (only one user request is processed).

Free the TCP port:

$httpListener.Close()

If you want the HTTP server to keep returning your page, you need to add PowerShell code to the loop. The following example starts an HTTP server in a loop that ends when any key is pressed in the PowerShell console.

write-host "Press any key to stop the HTTP listener after next request"
while (!([console]::KeyAvailable)) {
$context = $httpListener.GetContext()
$context.Response.StatusCode = 200
$context.Response.ContentType = 'text/HTML'
$WebContent = Get-Content -Path "C:\PS\testwebpage.html" -Encoding UTF8
$EncodingWebContent = [Text.Encoding]::UTF8.GetBytes($WebContent)
$context.Response.OutputStream.Write($EncodingWebContent , 0, $EncodingWebContent.Length)
$context.Response.Close()
Write-Output "" # Newline
}
$httpListener.Close()

A PowerShell HTTP server remains alive until you close the PowerShell console or end the session by using the .Close method.

You can run this PowerShell script as a Windows service.

You can run such a lightweight web server on any Windows host without Internet Information Services or other third-party software installation. No administrator privileges are required. You may use this HTTPListener as a simple REST server or to get information from a remote computer via HTTP.

0 comment
1
Facebook Twitter Google + Pinterest
previous post
Turn Linux Computer into Wi-Fi Access Point (Hotspot)
next post
Configuring DNS Conditional Forwarding and DNS Policies on Windows Server

Related Reading

Configuring Event Viewer Log Size on Windows

May 24, 2023

How to Detect Who Changed the File/Folder NTFS...

May 24, 2023

Enable Single Sign-On (SSO) Authentication on RDS Windows...

May 23, 2023

Allow Non-admin Users RDP Access to Windows Server

May 22, 2023

How to Create, Change, and Remove Local Users...

May 17, 2023

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Configuring Event Viewer Log Size on Windows

    May 24, 2023
  • How to Detect Who Changed the File/Folder NTFS Permissions on Windows?

    May 24, 2023
  • Enable Single Sign-On (SSO) Authentication on RDS Windows Server

    May 23, 2023
  • Allow Non-admin Users RDP Access to Windows Server

    May 22, 2023
  • How to Create, Change, and Remove Local Users or Groups with PowerShell?

    May 17, 2023
  • Fix: BSOD Error 0x0000007B (INACCESSABLE_BOOT_DEVICE) on Windows

    May 16, 2023
  • View Success and Failed Local Logon Attempts on Windows

    May 2, 2023
  • Fix: “Something Went Wrong” Error When Installing Teams

    May 2, 2023
  • Querying Windows Event Logs with PowerShell

    May 2, 2023
  • Configure Windows LAPS (Local Administrator Passwords Solution) in AD

    April 25, 2023

Follow us

  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Configuring Port Forwarding in Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • How to Delete Old User Profiles in Windows?
  • Adding Drivers into VMWare ESXi Installation Image
  • Configuring SFTP (SSH FTP) Server on Windows
  • How to Find the Source of Account Lockouts in Active Directory?
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top