0

Executing the following script saved in a *.ps1 file, either by opening it from a brand new PowerShell instance or by right-clicking and doing "Run with PowerShell" gives an unable to find type [System.Windows.Forms.CheckBox] error.

Add-Type -AssemblyName System.Windows.Forms

class Test {
    [System.Windows.Forms.CheckBox]$Box
}

$test = [Test]::new()
$test.Box = New-Object System.Windows.Forms.CheckBox
$test.Box.Text = "Testing"
Write-Output $test.Box.Text

Interestingly, opening a new PowerShell console, manually typing Add-Type -AssemblyName System.Windows.Forms and then executing the script does work. Is there any gotcha about Add-Type when used from inside a *.ps1 script?

Matrefeytontias
  • 592
  • 1
  • 4
  • 13

0 Answers0