Now also on Powershell Gallery as WebServer module, see here or install with Install-Module Webserver. Project page on github is here.

  

 

(also on Powershell Gallery as part of the SysAdminsFriends module, see here or install with Install-Module SysAdminsFriends)

 

Since Technet Gallery will be closed soon, you find the script here now:

 https://github.com/MScholtes/TechNet-Gallery  

 

Update 1.2, 2019-08-30:

- serves directory listings for directories under the base path if no index.htm[l] or default.htm[l] is present

- if no base directory is passed as parameter, the current directory is used as base path

 

The .Net class System.Net.HttpListener delivers a basic web service without the need to install a webserver (IIS) role. Since we can use it in C#, we can do it with powershell too (and even without pain).

There are several examples of powershell webservers in the internet, but for me it seems they all have the same origin. Thank you, original coder, whoever you are! My impulse to write a powershell webserver was a short but very impressing example on powershell.com (that unfortunately is no longer online).

 

The script Start-Webserver.ps1 implements a webserver with the following functions:

- a powershell command execution web form

- powershell script upload and execution (as a function)

- download files from the server

- upload files to the server

- make the webserver beep (to find servers in the datacenter)

- show webserver logs

- show server starttime and current time

- stop the webserver

- deliver static content based on the script's directory if none of the commands above is requested

The webserver responds to GET methods only except the POST upload and download functionalities (implementing this really did hurt).

Other functions can be implemented very easily.

If you give it a try, see remarks below by all means.

 

Update 2017-11-29: implemented delivery of static content if no "active" command is requested

Update 2016-10-24: introduced a form with POST method for downloading files (just for fun and practise).

 

Examples:

Start webserver with binding to http://localhost:8080/ (assuming the script is in the current directory):

PowerShell
Edit|Remove
.\Start-Webserver.ps1
 
Start webserver with binding to all IP addresses of the system and port 8080 (assuming the script is in the current directory).
Administrative rights are necessary:
PowerShell
Edit|Remove
.\Start-Webserver.ps1 "http://+:8080/"
 
Start powershell webserver as scheduled task as user local system every time the computer starts (when the correct path to the file Start-WebServer.ps1 is given):
Bash/shell
Edit|Remove
schtasks.exe /Create /TN "Powershell Webserver" /TR "powershell -file C:\Users\Markus\Documents\Start-WebServer.ps1 http://+:8080/" /SC ONSTART /RU SYSTEM /RL HIGHEST /F
You can start the webserver task manually with
  schtasks.exe /Run /TN "Powershell Webserver"

Delete the webserver task with
  schtasks.exe /Delete /TN "Powershell Webserver"

(Scheduled tasks are always running with low priority, so some functions might be slow)

 

 

Remarks:

Firewall

You may have to configure a firewall exception to allow access to the chosen port, e.g. with:
  netsh advfirewall firewall add rule name="Powershell Webserver" dir=in action=allow protocol=TCP localport=8080

After stopping the webserver you should remove the rule, e.g.:
  netsh advfirewall firewall delete rule name="Powershell Webserver"

 

How to enable https encryption

You can also let the Powershell Webserver deliver encrypted traffic. For a description look here.

  

 

Security

There is no security!!! Once started the webserver can be accessed by everyone and everyone can do on the webserver system what you can do.

 

Download / Upload of large files

There is no optimization for large file download or uploads (no chunking or similiar techniques). The transfer of large files might work, or might not.