Run a web server in one line of Powershell

Here’s how to serve a static single page website in one line of Powershell.

while(1){catindex.html|nc-w1-l-p8080}

Where index.html is the file you want to serve, in your current working directory. Open a browser to http://localhost:8080 to see the “website”.

This requires netcat, which you can install with Scoop by running scoop install netcat, or download from Jon Craton here.

The -w1 option makes netcat close the connection after 1 second, which seems necessary for the Windows version of netcat. Unlike the Unix version, it doesn’t automatically close the connection when it reaches the end of the piped input. So unfortunately this is a very slow web server, unless someone knows how to fix this?

For kicks, here’s how to serve a “dynamic website” that reports the current time:

while(1){"The time is $([datetime]::now)"|nc-w1-l-p8080}Tryhittingrefreshinyourbrowserafewtimestoconfirmthatthisisreallyworking.