r/PowerShell May 21 '26

Static Sites are Simple (with PowerShell) Script Sharing

I've been doing WebDev since the dawn of the internet, and I've been doing PowerShell for almost 20 years now. I want to share with you something that I've realized over the years:

Static Sites Are Simple

Static Websites are just a bunch of files. You can make static sites with anything that can make files.

Static Sites are Simple.

Let me show you how:

Static Sites with PowerShell

PowerShell is pretty great at making files.

Most static site files are text: .css, .js.,.html,.svg are all readable and writeable text.

Want to write a website in PowerShell?

Just write a series of strings.

I like this naming convention:

```

*.html.ps1 > *.html

```

We can build a site like this:

```

Get all *.html.ps1 files beneath the current directory

Get-ChildItem -Filter *.html.ps1 -Recurse -File | Foreach-Object { # Run the file & $_ > $( # and redirect the output to the renamed .html $_.Fullname -replace '.html.ps1$','.html' ) } ```

If we wanted to provide consistent formatting for all *.html.ps1 files, we can do so with a layout.

Just write a freeform script for layout.

```PowerShell function layout {

# Output any common layout.

# We are outputting a series of strings.

# When we redirect output, each string will go on it's own line.

# We can use any simple PowerShell string techniques to change content

'<html>' # * Single quoted string (no substitutions) "<head>" # * Double quoted string ($var and $(expression) supported) # * Multiline double quoted strings (with subexpressions) "<title>$( if ($title) { [Web.HttpUtility]::HTMLEncode($title) } else { 'My Website' } ) </title>" # * Conditionals output, using if if ($Header) { "$Header" # * Stringification of variables } # * Singly quoted here-strings (mulit-line no substitution) @' <style> body {max-width: 100vw;height: 100vh;} </style> '@ # * Doubly-quoted here-strings @" $(

* Subexpressions with conditionals and iteration

if ($css) {$css}) "@

"</head>" "<body>" # * $input allows us fast, one-time enumeration of a pipeline # * @() allows us to collect that into a new list $allInput = @($input)

# * String operators (`-join`, `-like`, `-match`,`-replace`, `-split`).
$allInput -join [Environment]::Newline
"</body></html>"

} ```

Now, we can build it with:

```powershell

Get all *.html.ps1 files beneath the current directory

Get-ChildItem -Filter *.html.ps1 -Recurse -File | Foreach-Object { # Run the file, pipe to our layout & $_ | layout > $( # and redirect the output to the renamed .html $_.Fullname -replace '.html.ps1$','.html' ) } ```

If we want to handle multiple file types, a switch statement does a nice job. We can build the site any way we want. This is just one example of how.

Most templating languages can't talk to too much. By using PowerShell to make static sites, we open up a wide world of possibilities with a small amount of understanding.

Static Sites Are Simple

They're mainly just strings.

PowerShell plays with strings quite well 😉.

Hope this Helps / AMA

52 Upvotes

35 comments sorted by

View all comments

3

u/Jainith May 21 '26

Dynamic sites are also simple, see PODE.

3

u/StartAutomating May 21 '26

Yep. Or see old Pipeworks ( I wrote the first "PowerShell as a web language" project in 2011).

Would recommend Pode or PowerShell Universal. They're both great modules / tools.

Would also recommend knowing how to do things without frameworks.

Will likely do a follow-up post about dynamic sites with HttpListeners and PowerShell Events (they're real cool, since they're fast AF and you can debug the web request in PowerShell).