Powershell GUI contents get distorted at execution RRS feed

  • Question

  • Hello,

    I have created a powershell script that contains only text and a push button.

    The purpose of the message box is to display a message to the user. On clicking the push button, the message gets dismissed.

    When executing the script from within the Powershell ISE, everything appears as expected.

    However, while executing the same script from the commandline, the message box items get displaced/distorted.

    How do I fix this ?


    Powershell Script


    Add-Type -AssemblyName System.Windows.Forms
    #region begin GUI{ 
    $NotifyEmailUpgrade              = New-Object system.Windows.Forms.Form
    $NotifyEmailUpgrade.ClientSize   = '1200,800'
    $NotifyEmailUpgrade.StartPosition= "CenterScreen"
    $NotifyEmailUpgrade.ShowInTaskbar= $False
    $NotifyEmailUpgrade.text         = "!! EMAIL UPGRADE !!"
    $NotifyEmailUpgrade.TopMost      = $true
    $NotifyEmailUpgrade.ControlBox   = $False
    $NotifyEmailUpgrade.AutoSize     = $False
    $NotifyEmailUpgrade.MaximizeBox  = $False
    $NotifyEmailUpgrade.FormBorderStyle = 'Fixed3D'
    $NotifyEmailUpgrade.SizeGripStyle= "Hide"
    $NotifyEmailUpgrade.MinimumSize  = '1200,800'
    $NotifyEmailUpgrade.MaximumSize  = '1200,800'
    $NotifyEmailUpgrade.BackColor    = "#d8ebf8"
    $Label1                          = New-Object system.Windows.Forms.Label
    $Label1.text                     = "Your email will be upgraded this weekend !!"
    $Label1.BackColor                = "#f8e71c"
    $Label1.AutoSize                 = $True
    $Label1.width                    = 25
    $Label1.height                   = 10
    $Label1.location                 = New-Object System.Drawing.Point(30,37)
    $Label1.Font                     = 'Calibri,15,style=Bold,Underline'
    $Label1.ForeColor                = "#d0021b"
    $Label2                          = New-Object system.Windows.Forms.Label
    $Label2.text                     = "•	For the best experience, be sure to"
    $Label2.AutoSize                 = $True
    $Label2.width                    = 25
    $Label2.height                   = 10
    $Label2.location                 = New-Object System.Drawing.Point(51,140)
    $Label2.Font                     = 'Calibri,13'
    $Label3                          = New-Object system.Windows.Forms.Label
    $Label3.text                     = "•	When you restart your computer, if you are prompted for credentials when accessing Outlook or Skype,"
    $Label3.AutoSize                 = $True
    $Label3.width                    = 25
    $Label3.height                   = 10
    $Label3.location                 = New-Object System.Drawing.Point(51,200)
    $Label3.Font                     = 'Calibri,13'
    $Label4                          = New-Object system.Windows.Forms.Label
    $Label4.text                     = "•	If you experience any issues with your migration, close and reopen the application"
    $Label4.AutoSize                 = $True
    $Label4.width                    = 25
    $Label4.height                   = 10
    $Label4.location                 = New-Object System.Drawing.Point(51,300)
    $Label4.Font                     = 'Calibri,13'
    $Label5                          = New-Object system.Windows.Forms.Label
    $Label5.text                     = "•	If you are unable to connect to Skype, please click"
    $Label5.AutoSize                 = $True
    $Label5.width                    = 23
    $Label5.height                   = 10
    $Label5.location                 = New-Object System.Drawing.Point(51,440)
    $Label5.Font                     = 'Calibri,13'
    $Label6                          = New-Object system.Windows.Forms.Label
    $Label6.text                     = "remember"
    $Label6.AutoSize                 = $True
    $Label6.width                    = 25
    $Label6.height                   = 10
    $Label6.location                 = New-Object System.Drawing.Point(65,240)
    $Label6.Font                     = 'Calibri,13'
    $Label7                          = New-Object system.Windows.Forms.Label
    $Label7.text                     = "where you experience the issue.  If the problem continues, reboot before contacting"
    $Label7.AutoSize                 = $True
    $Label7.width                    = 25
    $Label7.height                   = 10
    $Label7.location                 = New-Object System.Drawing.Point(65,340)
    $Label7.Font                     = 'Calibri,13'
    $Label8                          = New-Object system.Windows.Forms.Label
    $Label8.text                     = "TechCare for additional assistance."
    $Label8.AutoSize                 = $True
    $Label8.width                    = 25
    $Label8.height                   = 10
    $Label8.location                 = New-Object System.Drawing.Point(65,380)
    $Label8.Font                     = 'Calibri,13'
    $Label9                          = New-Object system.Windows.Forms.Label
    $Label9.text                     = "to use your email address for your username"
    $Label9.AutoSize                 = $True
    $Label9.width                    = 25
    $Label9.height                   = 10
    $Label9.location                 = New-Object System.Drawing.Point(188,240)
    $Label9.Font                     = 'Calibri,13,style=Bold,Underline'
    $Label9.ForeColor                = "#d0021b"
    $Label10                         = New-Object system.Windows.Forms.Label
    $Label10.text                    = ",not your other ID."
    $Label10.AutoSize                = $True
    $Label10.width                   = 25
    $Label10.height                  = 10
    $Label10.location                = New-Object System.Drawing.Point(690,241)
    $Label10.Font                    = 'Calibri,13'
    $Label11                         = New-Object system.Windows.Forms.Label
    $Label11.text                    = "Cancel Sign In > Delete Sign in Credentials,"
    $Label11.AutoSize                = $True
    $Label11.width                   = 25
    $Label11.height                  = 10
    $Label11.location                = New-Object System.Drawing.Point(610,440)
    $Label11.Font                    = 'Calibri,13,style=Bold,Underline'
    $Label11.ForeColor                = "#d0021b"
    $Label12                         = New-Object system.Windows.Forms.Label
    $Label12.text                    = "then retry."
    $Label12.AutoSize                = $True
    $Label12.width                   = 25
    $Label12.height                  = 10
    $Label12.location                = New-Object System.Drawing.Point(65,480)
    $Label12.Font                    = 'Calibri,13'
    $Label13                         = New-Object system.Windows.Forms.Label
    $Label13.text                    = "If problems persist, contact the technology service channel of your choice:"
    $Label13.AutoSize                = $True
    $Label13.width                   = 25
    $Label13.height                  = 10
    $Label13.location                = New-Object System.Drawing.Point(30,560)
    $Label13.Font                    = 'Calibri,13'
    $Label14                         = New-Object system.Windows.Forms.Label
    $Label14.text                    = "chat, online ticket, in-person or phone"
    $Label14.AutoSize                = $True
    $Label14.width                   = 25
    $Label14.height                  = 10
    $Label14.location                = New-Object System.Drawing.Point(30,600)
    $Label14.Font                    = 'Calibri,13'
    $Label15                         = New-Object system.Windows.Forms.Label
    $Label15.text                    = "log-off from your computer before you leave work for the weekend."
    $Label15.AutoSize                = $True
    $Label15.width                   = 25
    $Label15.height                  = 10
    $Label15.location                = New-Object System.Drawing.Point(445,140)
    $Label15.Font                    = 'Calibri,13,style=Bold,Underline'
    $Button1                         = New-Object system.Windows.Forms.Button
    $Button1.BackColor               = "#9b9b9b"
    $Button1.text                    = "OK"
    $Button1.width                   = 200
    $Button1.height                  = 87
    $Button1.location                = New-Object System.Drawing.Point(950,650)
    $Button1.Font                    = 'Calibri,11,style=Bold'
    #region gui events {
    $Button1.Add_MouseClick({ $NotifyEmailUpgrade.Close() })
    $Button1.Add_Click({ $NotifyEmailUpgrade.Close() })
    #endregion events }
    #endregion GUI }
    #Write your logic code here

    When executing this script from within the PowerShell ISE, it is displayed as follows:


    When I execute this script using the following commandline, all the GUI components get distorted.

    The alignment is lost.

    powershell.exe -ExecutionPolicy Bypass -NoExit -File "Path to PS1 file"

    • Edited by Soumen Ray Tuesday, August 14, 2018 12:23 PM Screenshots added.
    • Moved by Bill_Stewart Wednesday, December 12, 2018 5:04 PM This is not "debug/fix/rewrite my script for me" forum
    Tuesday, August 14, 2018 12:25 AM

All replies

  • Please post code correctly using the code posting tool provided.


    Tuesday, August 14, 2018 12:55 AM
  • I see no difference.  The screen is very badly designed and laid out.  Most of the code is unnecessary.  Use an RTF control and use Word to create the colorful message then export to an rtf file.  You can store the RTF in the script.


    Tuesday, August 14, 2018 1:01 AM
  • I have now posted the code correctly and included screenshots as well. Let me know if you have any suggestions.
    Tuesday, August 14, 2018 12:25 PM
  • Hi Soumen,

    I'm afraid I agree with JRV here. The code is laid out in a weird way and has a lot of excess and unnecessary code. For example, the max and min sizes for the form make no sense since you have set the style as "fixed3D" which doesn't allow resizes.

    let us know if we're missing anything because the code runs just fine for me either in the ISE or using a prompt.

    Tuesday, August 14, 2018 12:38 PM
  • Powershell ISE vs Commandine, my observations are very different, just like the attached screen grabs. I have tested this on almost 20 Windows 10 machines until now, running Powershell v5.x.

    I have no clue how to get this worked out.

    Tuesday, August 14, 2018 2:39 PM
  • Your code makes assumptions about how controls work and how they are sized.  The code ignores the way things flow in the painting of forms.

    To get the form to behave you will need to use fixed sizing and you will need to design that in the CLI version as the ISE resets the way forms are managed and displayed.  Since you are writing a script to use outside of the ISE do not design in the ISE.

    The easier method is to use one control - a RichTextBox.  Use word to design the colorful message.  This will display better in both programs.

    Also, if you are using hi-dpi monitors there are many other things that must be coded for accurate positioning.

    Note that setting "AutoSize" on labels will further aggravate your issue.


    • Edited by jrv Tuesday, August 14, 2018 4:04 PM
    Tuesday, August 14, 2018 4:03 PM