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.
Thanks to help from Avshalom, I was able to figure this out: After an initial call to
Add-Typein 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-Typeis still required. Otherwise, the following error will be thrown:Following the single
Add-Typestatement in question, the assemblies shown below were all loaded into the session: