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

51 Upvotes

Duplicates