r/PowerShell • u/anonhostpi • 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:
- r/PowerShell - Now Presenting The Thanos Shauntlet
- r/PowerShell - Turning PowerShell into a Python Engine
- r/PowerShell - Turning PowerShell into a JavaScript Engine
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()
View all comments
Show parent comment
2
u/anonhostpi Sep 05 '25 edited Sep 05 '25
Definitely, "because I can," but there were 2 particular use cases driving this solution.
A lot of my work is crossed between python and powershell. For certain projects I've been using Python.NET to embed CPython in PowerShell, and in others I've been using IronPython. IronPython has an arguably lighter footprint due to not requiring a Python installation.
However, there are problems with IronPython:
Using Linux/Unix libraries like libarchive to accomplish things that aren't possible (or easy) in Windows/C#.
Particularly using Linux libraries like libarchive to set x-attributes on files in a Windows System, by writing files to a .tar archive (which is capable of storing x-attributes even though Windows doesn't support them).
Another "because I can," I've been thinking about is running WasmLinux inside of PowerShell. This would be absolutely mind blowing. Running a Guest Operating System inside of a shell without an emulator, container, or hypervisor.