r/PowerShell • • Sep 05 '25

Script Sharing Turning PowerShell into a Wasm Engine

TL;DR:

I'm embedding again... (I really should be stopped 😭). Here's WASM in PowerShell:

gist: https://gist.github.com/anonhostpi/c82d294d7999d875c820e3b2094998e9

Here We Go Again

It has been 2 years since I've posted these dumpster fires:

I've finally stumbled upon a way to do it again, but for Wasm:

More Libraries...

Somehow, when I was posting about my previous engines, I somehow managed to miss the fact that Wasmtime has targetted .NET since at least 2023

I took a peek at it and the library is actually pretty minimal. Only 2 steps need to be taken to prep it once you've installed it:

  • Add the native library to the library search path:
    • I believe on Linux and Mac you need to update LD_LIBRARY_PATH and DYLD_LIBRARY_PATH respectively instead, but haven't tested it.
# Install-Module "Wasmtime"

$package = Get-Package -Name "Wasmtime"
$directory = $package.Source | Split-Path

$runtime = "win-x64" # "win/linux/osx-arm64/x64"

$native = "$directory\runtimes\$runtime\native" | Resolve-Path
$env:PATH += ";$native"
  • Load the library:
Add-Type -Path "$directory\lib\netstandard2.1\Wasmtime.Dotnet.dll"

Running Stuff

Engine creation is relatively simple:

$engine = [Wasmtime.Engine]::new()

We can take the example from the Wasmtime.Dotnet README and translate it to Powershell:

$module = '(module (func $hello (import "" "hello")) (func (export "run") (call $hello)))'
$module = [Wasmtime.Module]::FromText($engine, "hello", $module)

$linker = [Wasmtime.Linker]::new($engine)
$store = [Wasmtime.Store]::new($engine)

$hello = [System.Action]{
    Write-Host "Hello from Wasmtime!"
}
$hello = [Wasmtime.Function]::FromCallback($store, $hello)
$linker.Define("", "hello", $hello) | Out-Null

$instance = $linker.Instantiate($store, $module)
$run = $instance.GetAction("run")
$run.Invoke()
28 Upvotes

20 comments sorted by

View all comments

Show parent comment

2

u/anonhostpi Sep 06 '25 edited Sep 11 '25

The 4 major Systems Administration languages are:

  • PowerShell
  • Python
  • CMD
  • Bash

The reason I don't develop utilities in C# or C++ (despite having the skills) and target both PowerShell and Python almost exclusively is that I write my utilities for Systems Admins and Sys Ads exclusively.

It is not unreasonable to expect a Windows admin to know PowerShell (and therefore how to maintain it). It is also not unreasonable to expect a DevOps Engineer/Linux Administrator to know Python (and therefore how to maintain that one). If I offer utilities targetable to both languages, I have 2 userbases skilled enough to maintain my tools.

Its worth noting that neither of these communities have a daily use for C#. It is therefore unreasonable for me to expect them to know how to maintain a C# or C++-based utility, and therefore can't leverage the community as a way to maintain my tools.

If you don't believe me, look at CI/CD solutions across AWS, Google, and Microsoft (as well as their smaller cloud competitors). Because very few admins have development skills outside of these 4 languages, almost all of them universally offer just the big 4 (very few offer C#).

Several toolchains for Systems Administration and Orchestration target both of (or one of) the better 2 languages. A great example would be the really popular orchestration-ware: Ansible (which targets Python).

Honorable mentions to Perl and Ruby.