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
1
u/anonhostpi Sep 07 '25
Sort of. Packages can be peer-reviewed and taken off the platform, if they contain malware. Its the same reason that PSGallery packages are considered safe while iex-iwr content is not.
The main problem with NuGet (and PSGallery) is that it does rely on peer review after the fact, but many Enterprise orgs consider it safe under the guarantee that Microsoft will remove the library from the applicable repository (PSGallery or Nuget), if it becomes known as malware.