Custum API erroring when using New-PSDrive

Product: PowerShell Universal
Version: 5.0.9

I’m creating a new API post endpoint.
The first line I’m calling New-PSDrive so I can access ConfigMgr commandlets.

I’m getting this error when it’s called.

Could not load file or assembly ‘System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=a99a5546f934e0k9’. The system cannot find the file specified.

I’m running the PowerShell 7 environment

It looks like the default PowerShell 7 environment doesn’t support windows forms.

I created a new environment using pwsh.exe.

Then it works.

Add-Type -Assembly System.Windows.Forms
[System.Windows.Forms.Form]

I created a new environment as suggested. However, now I’m getting this error

Assembly with same name is already loaded