1

I made some code that uses the New-Object command from the command line Powershell, but this error popped up whenever I tried to load anything from System.Windows:

PS C:\Users\USER> New-Object System.Windows.FontWeight
New-Object : Cannot find type [System.Windows.FontWeight]: verify that the assembly containing this type is loaded.
At line:1 char:1
+ New-Object System.Windows.FontWeight
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

This code works fine in Powershell ISE:

PS C:\Users\USER> New-Object System.Windows.FontWeight
Normal

If anyone can tell me what the problem is and how to fix it, that would be greatly appreciated!

3 Answers 3

1

The problem is, that the assembly PresentationCore that contains type System.Windows.FontWeight is not loaded in your PowerShell session. Seems like PowerShell ISE and PowerShell Console do not preload the same assemblies. You can load the corresponding assembly as follows:

Add-Type -AssemblyName PresentationCore
Sign up to request clarification or add additional context in comments.

Comments

0

PowerShell ISE loads the containing assembly on startup to build its own GUI.

In powershell.exe you'll have to load it yourself:

Add-Type -AssemblyName PresentationCore |Out-Null

Comments

0

The FontWeight Structure is present in the System.Windows namespace and PresentationCore assembly. As evident from the error, you need to add the assembly PresentationCore to your PowerShell command line. Use this and you will be good to go.

Add-Type -Assembly PresentationCore

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.