Building a Static Website with PowerShell
Building static sites with PowerShell is easy!
Seriously. This site is built with simple PowerShell
PowerShell is pretty powerful when it comes to text processing, and static sites are almost entirely text.
Hello World
Let's start very simple, with a customizable Hello World
# Set our message
$Message = "Hello World!"
# Generate a string that embeds our message, and redirect it to a file
"<h1>$Message</h1>" > MyFirstPowerShellPage.html
# use Invoke-Item to open the file in the default browser
Invoke-Item ./MyFirstPowerShellPage.html
When you run this code, it will create a file called MyFirstPowerShellPage.html in the current directory.
When you open that file in a browser, you'll see the text 'Hello World!' displayed.
Once more, with Markdown
PowerShell core includes a ConvertFrom-Markdown cmdlet, which can be used to generate HTML from Markdown.
Markdown is a simple markup language that can be used to format text.
Here's an example of how to use ConvertFrom-Markdown to generate HTML:
ConvertFrom-Markdown -InputObject "### Hello From Markdown!" |
Select-Object -ExpandProperty HTML
Hello From Markdown!
Since GitHub Workflows allow us to run PowerShell scripts, this is all we need to do to build a static site.
Now, let's get a little meta. We're going to include the source for this page in this page
To to this, we can use the MyInvocation automatic variable to get the script block for this page, like so:
The full code is below:
$Title = "How to Build Static Websites with PowerShell"
"<h1>Building a Static Website with PowerShell</h1>"
"<h2>Building static sites with PowerShell is easy!</h2>"
"<h3>Seriously. This site is built with simple PowerShell</h3>"
"<p>PowerShell is pretty powerful when it comes to text processing, and static sites are almost entirely text.</p>"
"<h2>Hello World</h2>"
"<p>Let's start very simple, with a customizable Hello World</p>"
"<pre><code class='language-PowerShell'>"
[Web.HttpUtility]::HtmlEncode({
# Set our message
$Message = "Hello World!"
# Generate a string that embeds our message, and redirect it to a file
"<h1>$Message</h1>" > MyFirstPowerShellPage.html
# use Invoke-Item to open the file in the default browser
Invoke-Item ./MyFirstPowerShellPage.html
})
"</code></pre>"
"<p>When you run this code, it will create a file called MyFirstPowerShellPage.html in the current directory.</p>"
"<p>When you open that file in a browser, you'll see the text 'Hello World!' displayed.</p>"
"<h3>Once more, with Markdown</h3>"
"<p>PowerShell core includes a ConvertFrom-Markdown cmdlet, which can be used to generate HTML from Markdown.</p>"
"<p>Markdown is a simple markup language that can be used to format text.</p>"
$LearnLink = 'https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-markdown?wt.mc_id=MVP_321542'
"<p>Here's an example of how to use <a href='$LearnLink'>ConvertFrom-Markdown</a> to generate HTML:</p>"
$markdownDemo = { ConvertFrom-Markdown -InputObject "### Hello From Markdown!" |
Select-Object -ExpandProperty HTML }
"<pre><code class='language-PowerShell'>"
$([Web.HttpUtility]::HtmlEncode($markdownDemo))
"</code></pre>"
. $markdownDemo
"<p>Since GitHub Workflows allow us to run PowerShell scripts, this is all we need to do to build a static site.</p>"
"<p>Now, let's get a little meta. We're going to include the source for this page in this page</p>"
"<p>To to this, we can use the <code>MyInvocation</code> automatic variable to get the script block for this page, like so:</p>"
"<p>The full code is below:</p>"
"<hr/>"
"<pre><code class='language-PowerShell'>"
[Web.HttpUtility]::HtmlEncode($MyInvocation.MyCommand.ScriptBlock)
"</code></pre>"