You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"Loading" of Windows Runtime assemblies fails in 7.1 Preview 4 #13042
/cc Steve Lee (@SteveL-MSFT) while this breaking change isn't PowerShell's fault, it will have a decent impact. It may be a good idea to look into adding built in support. C#/WinRT package seems to be the recommendation, though at a glance it doesn't look like that can be used as a general solution.
It may be that my morning coffee is only half drunk atm, but I'm struggling to wrap my head around making use of C#/WinRT in (non-C#) PowerShell.
From what I can see on the Gallery, the biggest use case of these WinRT assemblies are Toast Notifications of various flavours (Even dbatools uses them).
If we could wrangle built-in support in PowerShell that'd be super 💜. If not, it looks like module authors will need to figure out runtime projection... in BurntToast's case that'll probably mean a version or two that caps its host support at 7.0 until I/someone figure it out.
PowerShell has never WinRT support. The fact that users could load WinRT assemblies previously does not mean that everything worked out of the box. Moreover, the engine explicitly blocks WinRT event processing (Also it seems the engine consider WinRT types as special case).
To restore previous behavior users have to explicitly install C#/WinRT package (it seems it is still not ready for general use).
Since no changes was made in PowerShell engine all modules used WinRT directly (by p/invoke) should continue to work.
Ilya (@iSazonov) I 100% agree that this break isn't PowerShell's fault or even necessarily it's responsibility to fix. It still might be a good idea though.
PowerShell has never WinRT support.
In the same way it never supported System.Text.StringBuilder I suppose but if that disappeared tomorrow it might be a good idea to add it back.
Moreover, the engine explicitly blocks WinRT event processing (Also it seems the engine consider WinRT types as special case).
It also blocks processing of event handlers with a return type other than void. I'm guessing there are similar technical and/or design reasons.
In the same way it never supported System.Text.StringBuilder I suppose but if that disappeared tomorrow it might be a good idea to add it back.
Technically we would need to reference C # / WinRT package. But now the package is not ready for release. Also I guess MSFT team prefer to reduce PowerShell distributive size.
Josh King (@Windos) If I understand correctly BurntToast doesn't work on latest PowerShell preview. Do you consider C # / WinRT package for BurntToast?
Ilya (@iSazonov) Yeah, BurntToast 100% relies on WinRT assemblies so this change in .NET will break the module in PS preview. I missed Preview 4 before the MSI got pulled, so will confirm this when Preview 5 is out.
I will be looking into C#/WinRT, but haven't had a chance to sit down and figure that out yet.
Another use case of WinRT assemblies that's worth to be considered are its bitmap decoders that support Apple's HEIF format. I'm not aware of any alternative provided by Microsoft.
Today, we’re releasing .NET 5.0 Preview 6. It contains a small set of new features and performance improvements. The .NET 5.0 Preview 4 post covers what we are planning to deliver with .NET 5.0. Most of the features are now in the product,
More generally, we also agreed that we should, as a rule, snap to .NET's decision on API surface area except where there is overwhelming evidence that something has additional value to PS users. E.g. WinForms/WPF are used heavily in the PS ecosystem, and make sense to continue including on Windows.
Based on this thread I've been able to successfully use some WinRT classes in PowerShell by importing the two dlls in this BurntToast folder, but I can't figure out how to get or build these dlls from the primary source. Are these on NuGet somewhere?
You can now fix this with the Import-Package module that I just published to PowerShell gallery.
Import-Package"microsoft.windows.sdk.net.ref"
It automatically installs (if not already installed) the Microsoft.Windows.SDK.NET.Ref package and imports the whole .nupkg into the PowerShell session.
Small warning though:
That Microsoft package does not make use of the NuGet "runtime" directory, so Import-Package will not know that this package is Windows-specific, and will try to import it, even if you are running it from PowerShell on Linux.
I am sorry, but can someone please explain to non-programmers how to take advantage of this fix (or some other workaround)? I do not know anything about assemblies, namespaces, runtimes, etc. In my heart, I'm a mere sysadmin writing my 10-20 lines scripts that sometimes need to tackle Windows APIs, and historically PowerShell has been exceptionally good in that.
E.g., considering the basic examples in the very first message in this thread (or slighly more complex code samples like this), what exactly needs to be changed so that they work in PS7? (And, preferably, still work in PS5.1).
And I apologize if this has been already documented. Unfortunately, Internet search for "winRT PowerShell" currently gives you this thread as the most relevant link and not some documentation page.
@PowerShell/powershell-committee reviewed this, we agreed that we will not be redistributing the WinRT assemblies with PowerShell 7.1. We also agreed to have documentation added to help PowerShell users that want to use WinRT.
I created doc issue but don't have specific information for this documentation. I hope the participants in this discussion will post steps for using WinRT there.
You can actually just run whatever code on the older PowerShell by wrapping stuff with eg powershell { } and yes it works with this issue. Its that simple to run the script code in another installed powershell version. I have pwsh as newer PS and powershell as older one. One result can be a script stuck running and that just means the code needs adjustment.
As a simple example. You can open PowerShell and run any PS version by typing the name for it eg in PS 5.1.x I can type pwsh in my instance: > $one = pwsh { get-host } and get the version info from PS 7.4.x into $one. This works the same in a script. After pressing enter/return, then can type $one to print the info. Could even do something like $one = pwsh {(get-host).Version} then if ($one -match "7.4.1") { "true" } else { "false" }. All of that kicks back to the fact that you can open your code anywhere in script in any installed PS version older or newer and bring stuff back.
Might as well also add that you can check PS version or whatever else in the scripts and have the scripts interact with multiple PS versions (I added some version example above that could be the start of that. match would need to be changed to a better means of matching). Just search the internet how to do it. I'm sure there's multiple things as I can come up with 2 ways at least in my head.
If you have an error like in OP, and an older installed PS version where it works, then you can do this as long as there's an old enough installed PS version available. You just need to figure out where to wrap your scripts or code to work best.
To me that is simpler and better than doing anything above and I use it but it depends on your situation (and opinion).
I can't help you further on this. Just giving a hint of something possible that you can do and adding another possibility here to deal with the issue =)
For the current version 7.5 it works like this.
Install a NuGet package, for example, for a current user (or, if you want, for everyone or even in a separate catalog):
Activity
SeeminglyScience commentedon Jun 27, 2020
This is due to removal of builtin support in dotnet, see dotnet/runtime#37672.
/cc Steve Lee (@SteveL-MSFT) while this breaking change isn't PowerShell's fault, it will have a decent impact. It may be a good idea to look into adding built in support. C#/WinRT package seems to be the recommendation, though at a glance it doesn't look like that can be used as a general solution.
There are some very popular modules that utilize winrt APIs (like BurntToast by Josh King (@Windos)).
Windos commentedon Jun 28, 2020
It may be that my morning coffee is only half drunk atm, but I'm struggling to wrap my head around making use of C#/WinRT in (non-C#) PowerShell.
From what I can see on the Gallery, the biggest use case of these WinRT assemblies are Toast Notifications of various flavours (Even dbatools uses them).
If we could wrangle built-in support in PowerShell that'd be super 💜. If not, it looks like module authors will need to figure out runtime projection... in BurntToast's case that'll probably mean a version or two that caps its host support at 7.0 until I/someone figure it out.
iSazonov commentedon Jun 29, 2020
PowerShell has never WinRT support. The fact that users could load WinRT assemblies previously does not mean that everything worked out of the box. Moreover, the engine explicitly blocks WinRT event processing (Also it seems the engine consider WinRT types as special case).
To restore previous behavior users have to explicitly install C#/WinRT package (it seems it is still not ready for general use).
Since no changes was made in PowerShell engine all modules used WinRT directly (by p/invoke) should continue to work.
SeeminglyScience commentedon Jun 29, 2020
Ilya (@iSazonov) I 100% agree that this break isn't PowerShell's fault or even necessarily it's responsibility to fix. It still might be a good idea though.
In the same way it never supported
System.Text.StringBuilderI suppose but if that disappeared tomorrow it might be a good idea to add it back.It also blocks processing of event handlers with a return type other than
void. I'm guessing there are similar technical and/or design reasons.iSazonov commentedon Jun 29, 2020
Technically we would need to reference C # / WinRT package. But now the package is not ready for release. Also I guess MSFT team prefer to reduce PowerShell distributive size.
iSazonov commentedon Jun 30, 2020
Josh King (@Windos) If I understand correctly BurntToast doesn't work on latest PowerShell preview. Do you consider C # / WinRT package for BurntToast?
Windos commentedon Jun 30, 2020
Ilya (@iSazonov) Yeah, BurntToast 100% relies on WinRT assemblies so this change in .NET will break the module in PS preview. I missed Preview 4 before the MSI got pulled, so will confirm this when Preview 5 is out.
I will be looking into C#/WinRT, but haven't had a chance to sit down and figure that out yet.
iSazonov commentedon Jun 30, 2020
Josh King (@Windos) You can load a nightly build from main page of the repo.
wi-fr commentedon Jun 30, 2020
Another use case of WinRT assemblies that's worth to be considered are its bitmap decoders that support Apple's HEIF format. I'm not aware of any alternative provided by Microsoft.
Windos commentedon Jul 1, 2020
Can confirm, expected exception when importing the module.
SteveL-MSFT commentedon Jul 1, 2020
I'll bring this up with the .NET team
AdamBraden commentedon Jul 1, 2020
With the breaking change in .NET5 Preview 6, https://devblogs.microsoft.com/dotnet/announcing-net-5-0-preview-6/, you will now need to reference a cswinrt generated interop assembly to call winrt apis. For the Windows WinRT apis, we've built a nuget package that provides this support, and will need to be referenced when building the module - https://www.nuget.org/packages/Microsoft.Windows.SDK.NET
richlander commentedon Jul 1, 2020
There are three big questions we need to answer:
13 remaining items
joeyaiello commentedon Jul 8, 2020
More generally, we also agreed that we should, as a rule, snap to .NET's decision on API surface area except where there is overwhelming evidence that something has additional value to PS users. E.g. WinForms/WPF are used heavily in the PS ecosystem, and make sense to continue including on Windows.
Windos commentedon Jul 8, 2020
Thanks for the quick decision on this. Will work on a module update to include the assemblies.
iSazonov commentedon Jul 9, 2020
/cc Sean Wheeler (@sdwheeler) for reference.
xdhmoore commentedon Mar 15, 2021
Based on this thread I've been able to successfully use some WinRT classes in PowerShell by importing the two dlls in this BurntToast folder, but I can't figure out how to get or build these dlls from the primary source. Are these on NuGet somewhere?
I'm attempting to update this library: https://github.com/rkeithhill/PoshWinRT which is a C# wrapper that can be imported in PowerShell in order to use APIs that use Async file operations.
Windos commentedon Mar 15, 2021
Daniel Moore - Looking for Work (@xdhmoore): https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/
PowerShell 7.1
anonhostpi commentedon Oct 8, 2023
You can now fix this with the Import-Package module that I just published to PowerShell gallery.
It automatically installs (if not already installed) the Microsoft.Windows.SDK.NET.Ref package and imports the whole .nupkg into the PowerShell session.
Small warning though:
That Microsoft package does not make use of the NuGet "runtime" directory, so Import-Package will not know that this package is Windows-specific, and will try to import it, even if you are running it from PowerShell on Linux.
pronichkin commentedon Mar 25, 2024
I am sorry, but can someone please explain to non-programmers how to take advantage of this fix (or some other workaround)? I do not know anything about assemblies, namespaces, runtimes, etc. In my heart, I'm a mere sysadmin writing my 10-20 lines scripts that sometimes need to tackle Windows APIs, and historically PowerShell has been exceptionally good in that.
E.g., considering the basic examples in the very first message in this thread (or slighly more complex code samples like this), what exactly needs to be changed so that they work in PS7? (And, preferably, still work in PS5.1).
And I apologize if this has been already documented. Unfortunately, Internet search for "winRT PowerShell" currently gives you this thread as the most relevant link and not some documentation page.
iSazonov commentedon Mar 26, 2024
Sean Wheeler (@sdwheeler) Have we the docs or a doc issue?
sdwheeler commentedon Mar 26, 2024
Ilya (@iSazonov) No we don't have docs or a docs issue. Please open a docs issue and provide the necessary information needed for the docs.
iSazonov commentedon Mar 26, 2024
I created doc issue but don't have specific information for this documentation. I hope the participants in this discussion will post steps for using WinRT there.
Xavron commentedon Apr 2, 2024
Artem Pronichkin (@pronichkin)
You can actually just run whatever code on the older PowerShell by wrapping stuff with eg
powershell { }and yes it works with this issue. Its that simple to run the script code in another installed powershell version. I have pwsh as newer PS and powershell as older one. One result can be a script stuck running and that just means the code needs adjustment.As a simple example. You can open PowerShell and run any PS version by typing the name for it eg in PS 5.1.x I can type pwsh in my instance: >
$one = pwsh { get-host }and get the version info from PS 7.4.x into $one. This works the same in a script. After pressing enter/return, then can type$oneto print the info. Could even do something like$one = pwsh {(get-host).Version}thenif ($one -match "7.4.1") { "true" } else { "false" }. All of that kicks back to the fact that you can open your code anywhere in script in any installed PS version older or newer and bring stuff back.Might as well also add that you can check PS version or whatever else in the scripts and have the scripts interact with multiple PS versions (I added some version example above that could be the start of that. match would need to be changed to a better means of matching). Just search the internet how to do it. I'm sure there's multiple things as I can come up with 2 ways at least in my head.
If you have an error like in OP, and an older installed PS version where it works, then you can do this as long as there's an old enough installed PS version available. You just need to figure out where to wrap your scripts or code to work best.
To me that is simpler and better than doing anything above and I use it but it depends on your situation (and opinion).
I can't help you further on this. Just giving a hint of something possible that you can do and adding another possibility here to deal with the issue =)
denis-komarov commentedon Mar 16, 2025
Artem Pronichkin (@pronichkin)
Ilya (@iSazonov)
For the current version 7.5 it works like this.
Install a NuGet package, for example, for a current user (or, if you want, for everyone or even in a separate catalog):
Now WinRT can be called like this: