Search code examples
powershelladd-type

How to add some types in PowerShell 6 (I can do in PowerShell 5)


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.


Solution

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