" $NR = (New-Object Syst" /> " $NR = (New-Object Syst" /> " $NR = (New-Object Syst"/>

Is Add-Type -AssemblyName PresentationFramework still required in PowerShell?

3.4k Views Asked by At

I have the following PowerShell code:

Add-Type -AssemblyName PresentationFramework
[xml]$Form  = "<XAML window definition here>"
$NR = (New-Object System.Xml.XmlNodeReader $Form)
$Win = [Windows.Markup.XamlReader]::Load($NR)
$Win.ShowDialog()

If I comment out the very first Add-Type statement, the script still works and displays the window UI.

The above script is from a training course dated 2016. I am wondering whether the Add-Type statement is no longer required due to changes in PowerShell since? Is the assembly in question included by default in the PowerShell environment? I tested using both ISE and VS Code, on Windows 10.

1

There are 1 best solutions below

0
Sabuncu On

Thanks to help from Avshalom, I was able to figure this out: After an initial call to Add-Type in a fresh PowerShell session, the assembly stays resident. This is why I was able to comment out the statement and still have it work.

But when that session is torn down and a new session is started, the assembly is no longer resident, and the corresponding Add-Type is still required. Otherwise, the following error will be thrown:

Unable to find type [Windows.Markup.XamlReader].

Following the single Add-Type statement in question, the assemblies shown below were all loaded into the session:

C:\WINDOWS\Microsoft.Net\assembly\GAC_64\PresentationCore\v4.0_4.0.0.0
C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Management\v4.0_3.0.0.0
C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework.Aero2\v4.0_4.0.0.0
C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework\v4.0_4.0.0.0
C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemXml\v4.0_4.0.0.0
C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Dynamic\v4.0_4.0.0.0
C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xaml\v4.0_4.0.0.0
C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\UIAutomationProvider\v4.0_4.0.0.0
C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\UIAutomationTypes\v4.0_4.0.0.0
C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0