Running Simple HTTP Web Server Using PowerShell |

I regularly need to run a simple web server on Windows, for testing purposes or as a simple stub in the service deployment phase. 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 on. In this example, I want to run an HTTP web server on port 9090.


Start Listener:


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, Windows shows up as a separate process waiting for a connection on port 9090. 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

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

<!DOCTYPE html>
Lightweight PowerShell Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
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;}
<h1>Test web page </h1>
<p>This web page was generated by PowerShell using the System.Net.HttpListener class</p>

HTML file for display on HTTP web server with PowerShell

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

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

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

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 contents of a web page. The script returns the HTML code only once, after which your listener will automatically stop (only one user request is processed).

Free TCP Port:


If you want the HTTP server to keep returning your page, you’ll 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
$WebContent = Get-Content -Path "C:\PS\testwebpage.html" -Encoding UTF8
$EncodingWebContent = [Text.Encoding]::UTF8.GetBytes($WebContent)
$context.Response.OutputStream.Write($EncodingWebContent , 0, $EncodingWebContent.Length)
Write-Output "" # Newline

A PowerShell HTTP server stays alive until you close the PowerShell console or end the session using .Close Method

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

Leave a Comment