A few months back I posted about how I identified a way to use WinUI 3 styling in WPF forms in PowerShell. However, in order to achieve it you had to download and sideload a 3rd party DLL. Well I have good news, that has been officially added to .NET 9 and is now available as of RC2!
I’ll cut straight to the chase, if you’d like to try this out now feel free to follow these steps (see below for longer term information that mitigates having to do most of these steps):
UPDATE 10/2/2024: PowerShell 7.5 Preview 5 has been released and prevents the need to follow these setup steps – https://github.com/PowerShell/PowerShell/releases/tag/v7.5.0-preview.5
- If you have installed any .NET 9 previews, be sure to uninstall those first as I noticed a conflict when I installed RC2 on top of other preview releases.
- Head over to where the nightly links are posted: https://github.com/dotnet/sdk/blob/main/documentation/package-table.md
- Download the “Installer” exe for RC 2 (main column): https://aka.ms/dotnet/9.0.1xx/daily/dotnet-sdk-win-x64.exe
- Run and install dotnet-sdk-9.0.100-rc.2.24426.11-win-x64.exe as admin
- Since PowerShell 7 doesn’t have a 7.5.x build on the RC yet, your only option currently is to use the fxdependent version.
- Head over to the PowerShell Releases page: https://github.com/PowerShell/PowerShell/releases/tag/v7.5.0-preview.3
- Download the latest fxdependent Win Desktop version for PowerShell 7.5: https://github.com/PowerShell/PowerShell/releases/download/v7.5.0-preview.3/PowerShell-7.5.0-preview.3-win-fxdependentWinDesktop.zip
- IMPORTANT: Older version are based on .NET 8 and older, and will typically not properly identify the .NET 9 installation
- Unzip and run pwsh.exe from the unzipped folder
- Note: If you just run pwsh.exe from the start menu or Run dialog, it will likely run the wrong version of PowerShell 7 due to your PATH environmental variable.
Now that you have setup your machine to be able to run the latest Windows 11 theming in WPF, here is how you can test this out for yourself:
- First off, if your pwsh.exe closes immediately it’s not identifying the RC 2 .NET 9 installation. The fxdependent version allows you to separately install the .NET 9 dependency, and it has built in logic to automatically identify it’s installation.
- You can verify that your PowerShell 7 instance is using the proper version of .NET 9 RC2 by running the following command:
[System.Runtime.InteropServices.RuntimeInformation]::FrameworkDescription
As long as it returns “.NET 9.0.0-rc.2.24423.10” or newer your environment is ready to go!
Finally, here is the sample you’ve been waiting for. Native .NET 9 RC 2 running Windows 11 style theming in a PowerShell 7 WPF Form!
Add-Type -AssemblyName PresentationFramework
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/PresentationFramework.Fluent;component/Themes/Fluent.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<TextBox x:Name="MyTextBox" Width="150" />
<Button x:Name="SubmitButton" Content="Submit" Margin="5" />
</StackPanel>
</StackPanel>
</Window>
"@
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
$window.ShowDialog()
While this is almost too good to be true, I recognize that the steps required to achieve it currently are not easily attainable. Especially for those in a corporate environment. So what’s next?
- Clearly given that PowerShell 7.5 will be based on .NET 9, it’s a bit of a waiting game for .NET 9 to go GA followed by a PowerShell release that incorporates that into it.
- At that point, you will be able to install a single PowerShell msi that contains both the required .NET 9 libraries and PowerShell 7.5.
- UPDATE 10/2/2024 – PowerShell 7.5 Preview 5 has been released – https://github.com/PowerShell/PowerShell/releases/tag/v7.5.0-preview.5
- In the meantime, I plan to begin work on migrating my C# based IT Admin Toolkit over to WPF. I should have a public GitHub repo available soon enough if you’d like to contribute.
Even typing out this article now, I almost can’t believe after years of seeking it out it’s finally something that admins can easily achieve without the need for 3rd party dlls, which makes adoptability of PowerShell based tools that much more attainable.