1
votes

I wrote a script to display a message box. It works well in PowerShell 5, but doesn't PowerShell 6. How can I make it work?

Script I wrote(extracted):

function test
{
     [CmdletBinding()]
     param([Parameter(Mandatory = $true, Position = 0)][string]$directory)

     Add-Type -AssemblyName System.Windows.Forms
     Add-Type -AssemblyName Microsoft.Visualbasic
     Add-Type -AssemblyName System.Drawing

  #----------------------------
}

Error message:

Add-Type : Cannot find path 'C:\Users\Owner\System.Windows.Forms.dll' because it does not exist. At C:\Users\Owner\Desktop\sample\trans-search.ps1:8 char:6 + Add-Type -AssemblyName System.Windows.Forms + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Users\Owner\System.Windows.Forms.dll:String) [Add-Type], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.AddTypeCommand

Thank you for your help.

1
I think they didn't work on porting Forms until .NET Core 3.0. You may be on an old version, or it's not fully ported yet. What's new in .NET Core 3.0 (Preview 6)Ash
Note that in ps5 you can do something like: using assembly System.Windows.Forms; using namespace System.Windows.Forms; [messagebox]::show('hello world')js2010
Maybe you can use a web browser to display things.js2010

1 Answers

2
votes

Those types aren't available in PowerShell 6.x. They are supposed to become available in PowerShell v7 but aren't in preview 2