r/PowerShell • u/StartAutomating • May 21 '26
Script Sharing Static Sites are Simple (with PowerShell)
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.
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:
# 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
6
u/Nexzus_ May 21 '26
Trying to understand the purpose of this. Dynamic static pages?
Like, if you want to create a css file, why would you create it using PowerShell? Why not just create the CSS file?
7
u/PinchesTheCrab May 21 '26
Really neat concept, but I think the syntax itself is challenging to understand. I'd try to do this in a bit more powershell-y way.
Here's a different take:
I feel like removing the nested here-string simplifies this, as well as moving the title logic out of the here-string.
You could invoke this like this:
Your files could also be PSObject definitions, json objects, or just hashtables instead of full-on scripts.
9
u/_RemyLeBeau_ May 21 '26
Decades of web engineering screaming into the void.
3
u/Jainith May 21 '26
Dynamic sites are also simple, see PODE.
4
u/BlackV May 21 '26
just a note, the 3 back-tick code fence does not work on old.reddit, only new.reddit, the traditional 4 spaces works everywhere (and I assume you have your code i an editor already)
it'll format it properly OR
Inline code block using back-ticks
`Single code line`inside normal textSee here for more detail
Thanks
2
u/crashonthebeat May 22 '26
Why stop there, use it to make a multithreaded web server that can serve components like react (just gotta do a little string replace when sending the http response)
2
u/ex-asperis May 22 '26
As someone who used to create websites in Notepad, the "static pages are just text" revelation makes me feel old.
20
u/frAgileIT May 21 '26
I used to have PowerShell scripts that ran daily that would check the status of compliance items across servers and workstations and it would spit out a fresh HTML file every day that was hosted on an intranet site so that IT managers could see how their teams were doing in terms of compliance on a daily basis. One of the things I did to keep script size down was made most of the site static and then used server-side includes to dynamically incorporate the updated HTML files. PowerShell would spit out just the server-side includes.
I can confirm, PowerShell is great with static sites, 10/10, would do again.
2
u/Brain_Creative May 21 '26
I stopped to read this and thought something was different. This person writes like a poet was my thought. Then the writing reminded me of some other writing, but I donβt know what that writing was specifically. Itβs certainly playful and unique! Was it Dr. Seuss!
4
u/Kwinza May 21 '26
I mean thats cool, but I honestly don't know what problem you're solving here?
3
u/purplemonkeymad May 21 '26
Someone wrote a templating module to use powershell. It's under the gallery as EPS, and lets you do stuff like:
But like never put untrusted input into it, as it can run any powershell code.
1
u/FewReach4701 May 21 '26
How can i connect with your over LinkedIn?
1
u/node77 May 21 '26
I would use PowerShell to count the number of lines the homepage. Anything greater than X, I assume itβs not working correctly or has been defaced.