PowerShell Windows Forms Project

Tool: Visual Studio
Version: 2022.10.0

I’ve created a basic PowerShell Windows Forms Project in visual studio and have added two buttons and a text box to the form. When I click on button 1 I want to exit the app and when I click on button 2 I want to display “hello ” on the screen. Could someone guide me on how to do this please?

I have tried using the Exit command inside the $button1_Click function but it doesn’t work and comes up with an unhandled exception.

Also, I’m not sure how to get the value of the textbox string into a powershell variable so that I can display the message.

Hi,
how are you importing the form into powershell?
I’m not familiar with basic forms but I use a lot of advanced .xaml forms

and to add a click I use something like:

Function stop-program
{
      $Form.Close()
      exit
}

$TestButton1.Add_Click({ stop-program })

for the textboxes I use

$MyVariable = $MyTextbox.Text

hope this helps

@Fred , I’m not importing into Powershell but created a new project from scratch in Visual Studio.

I tried your suggestion but it still comes up with an error:

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.Management.Automation.ExitException: System error.
at System.Management.Automation.Interpreter.ThrowInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean createLocalScope, Dictionary2 functionsToDefine, List1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object args)
at System.Management.Automation.ScriptBlock.<>c__DisplayClass57_0.b__0()
at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action action)
at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop, List1 variablesToDefine, Dictionary2 functionsToDefine, Object args)
at System.Management.Automation.ScriptBlock.InvokeAsDelegateHelper(Object dollarUnder, Object dollarThis, Object args)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4515.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll

Microsoft.PowerShell.ConsoleHost
Assembly Version: 3.0.0.0
Win32 Version: 10.0.22000.1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.ConsoleHost/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.ConsoleHost.dll

System
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4536.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll

System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4566.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll

System.Management.Automation
Assembly Version: 3.0.0.0
Win32 Version: 10.0.22000.832
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Management.Automation/v4.0_3.0.0.0__31bf3856ad364e35/System.Management.Automation.dll

Microsoft.Management.Infrastructure
Assembly Version: 1.0.0.0
Win32 Version: 10.0.22000.1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Management.Infrastructure/v4.0_1.0.0.0__31bf3856ad364e35/Microsoft.Management.Infrastructure.dll

System.Management
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4435.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Management/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Management.dll

System.DirectoryServices
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4501.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.DirectoryServices/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.DirectoryServices.dll

System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4161.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll

System.Numerics
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4161.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll

System.Data
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4536.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_64/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll

System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4341.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll

Anonymously Hosted DynamicMethods Assembly
Assembly Version: 0.0.0.0
Win32 Version: 4.8.4515.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_64/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll

Microsoft.PowerShell.Security
Assembly Version: 3.0.0.0
Win32 Version: 10.0.22000.1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.Security/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.Security.dll

System.Transactions
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4161.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_64/System.Transactions/v4.0_4.0.0.0__b77a5c561934e089/System.Transactions.dll

PowerShellProTools.VSCode
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/program%20files/microsoft%20visual%20studio/2022/community/common7/ide/extensions/ryrfhtey.a2w/PowerShellProTools.VSCode.dll

netstandard
Assembly Version: 2.0.0.0
Win32 Version: 4.8.4161.0
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/netstandard/v4.0_2.0.0.0__cc7b13ffcd2ddd51/netstandard.dll

PSEventHandler
Assembly Version: 0.0.0.0
Win32 Version: 10.0.22000.832
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Management.Automation/v4.0_3.0.0.0__31bf3856ad364e35/System.Management.Automation.dll

PSEventHandler
Assembly Version: 0.0.0.0
Win32 Version: 10.0.22000.832
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Management.Automation/v4.0_3.0.0.0__31bf3856ad364e35/System.Management.Automation.dll

Microsoft.PowerShell.Commands.Utility
Assembly Version: 3.0.0.0
Win32 Version: 10.0.22000.1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.Commands.Utility/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.Commands.Utility.dll

System.Configuration.Install
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4161.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration.Install/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.Install.dll

Microsoft.CSharp
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4161.0
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.CSharp/v4.0_4.0.0.0__b03f5f7f11d50a3a/Microsoft.CSharp.dll

Microsoft.PowerShell.Commands.Management
Assembly Version: 3.0.0.0
Win32 Version: 10.0.22000.1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.Commands.Management/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.Commands.Management.dll

System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4395.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll

System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4550.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll

Accessibility
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4161.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll

System.Web.Extensions
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4494.0
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Web.Extensions/v4.0_4.0.0.0__31bf3856ad364e35/System.Web.Extensions.dll

System.Web
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4494.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_64/System.Web/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Web.dll

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

@Fred , are there any samples of the advanced .xaml forms available?