3

I was testing popups with PowerShell when I noticed something weird occurring.

The icon on the left of the popup looked different compared to another method of showing it. Also, the button styles were different.

These were both run on the same system (Windows 10 Pro), and the code is exactly the same (shown below). Both were run on PowerShell 5.1.

Add-Type -AssemblyName PresentationFramework

$result = [System.Windows.MessageBox]::Show("Would you like to continue?", "Continue?", "YesNo", "Information", "No")
Write-Host $result

1. Old Icon and Buttons

The old icon and buttons were created using the typical PowerShell application. I saved the file as a .ps1 file and then ran it like usual.

$ ./test.ps1

However, it gave me this popup. It's normal, but it looks more like a Windows 7 popup than Windows 10.

Old Popup


2. New Icon and Buttons

The new icon and buttons were created using the Windows PowerShell ISE. It was run without saving the file (just clicked the Run button to execute it).

This gave me a popup I would expect to see on Windows 10.

New Popup


So, is this behaviour expected? Also, why is it happening?

Arnav Thorat
  • 3,078
  • 3
  • 8
  • 33
  • 2
    [Windows Forms look different in Powershell and Powershell ISE. Why?](https://stackoverflow.com/a/3359274/1701026) – iRon May 15 '22 at 08:09
  • Thanks, I'll try it out! But why is it occurring? What's the difference between PowerShell and PowerShell ISE? – Arnav Thorat May 15 '22 at 08:44
  • 2
    The ISE's default is already `EnableVisualStyles()` – iRon May 15 '22 at 09:04
  • Thanks for the clarification. If you want, you can post it as an answer, and I'll accept it! (+1 for your comments!) – Arnav Thorat May 15 '22 at 09:43
  • 2
    See [__Remarks__](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.application.enablevisualstyles?view=windowsdesktop-6.0#remarks) to get a better understanding on the __Why__ – Santiago Squarzon May 15 '22 at 16:36
  • 2
    **REOPEN REASON:** The reason I have voted to reopen my own question is because the linked question has the solution, but it doesn't have the information of why this is occurring. An answer explaining the _"Why?"_ part of the question would greatly benefit me and the community. Thank you for reading. – Arnav Thorat May 17 '22 at 08:00
  • 1
    @iRon I have re-opened this one since Keith Hill's answer is lacking on the __Why__ part of the answer, if you're are willing to provide an answer on this please free to do so – Santiago Squarzon May 20 '22 at 02:45
  • 1
    @Santiago, as I see it: *the question* is clearly a duplicate (also note the **Why?** In the title), which means you could have added your answer there or the questioner (@Arnav) could have 'start a bounty' on that question and/or answered there if it is in fact not fully answered. – iRon May 20 '22 at 07:02

1 Answers1

2

Solution

You need to add the following line to your code before the popup.

[System.Windows.Forms.Application]::EnableVisualStyles()

This will give the popup the new, modern feel to it.


Why?

The reason that this is occurring is because when running this in the PowerShell ISE, it automatically runs it with the EnableVisualStyles option.

However, when the code is run in PowerShell, it doesn't add the EnableVisualStyles part unless you specify so.

This is why the popup looks "older" in PowerShell, but more modern in the PowerShell ISE.

Arnav Thorat
  • 3,078
  • 3
  • 8
  • 33
  • 1
    Anyways, `+1` for the better answer than the [accepted answer](https://stackoverflow.com/a/3359274/1701026) in [Windows Forms look different in Powershell and Powershell ISE. Why?](https://stackoverflow.com/a/3359274/1701026) – iRon May 20 '22 at 08:20