Event Handlers Invalid cast

Feb 4, 2012 at 3:06 AM


I have recently installed powershell 3.0 and ShowUI (great tool thank you) on my system, however I am experiencing some unexpected behavior.

Any event handler I use generates an invalid cast error. Example Code: (Sorry for some reason the insert code option is not responding for me - using chrome)


stackpanel -ControlName sp1 -Children{   

button -Content "Click" -name button1 -On_Click{$rLabel.Content = get-random} -On_MouseEnter{

$rLabel.Background = "Black"

$rLabel.Foreground = "White"   


label -name rLabel

} -show



Specified cast is not valid.

Error in On_MouseEnter Event Handler on button1 (Button)

 At line:3 char:1+ $ErrorActionPreference = 'stop'

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Specified cast is not valid.Error in On_Click Event Handler on button1 (Button)

At line:3 char:1+ $ErrorActionPreference = 'stop'

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Feb 4, 2012 at 6:28 PM

What version of ShowUI are you using? (You can check with Get-Module | fl )

Feb 5, 2012 at 2:19 AM

It is version 1.3. 

When I first had this problem, I removed the module and re-downloaded the latest release from codeplex.

Is there a specific file within the module I could check for this?

Apr 24, 2012 at 10:28 PM

I have this problem too, it doesnt work correctly in powershell 3.0, which is a real shame as 2.0 ISE crashes a lot for me with ShowUI.

Anyone found a fixed for this yet?

Apr 25, 2012 at 5:52 AM

That exact code is working for me in a post-beta release of PowerShell 3 with ShowUI 1.3, so I'm going to go with: just wait a minute, it will change...

Apr 25, 2012 at 10:49 AM
Edited Apr 25, 2012 at 10:51 AM

Yes the code works fine, but every time you click the button it outputs an error in the console. You don't see any errors at all?

I dont know where it gets this from:

At line:3 char:1
+ $ErrorActionPreference = 'stop'

Even if you try to override this with SilentlyContinue it doesn't work.

Are there any plans to update ShowUI for PS 3.0?

Anyone know when PS 3.0 is going to be released out of beta?

Apr 26, 2012 at 1:58 AM

The ErrorActionPreference is ShowUI magic, because otherwise you wouldn't get any notice that you had an error in the event handler ... we add an error handler, and set it to stop so we can trap errors.

The script works as is on my build (you'll just have to wait for the next public CTP of PowerShell 3)