Skip to content

Instantly share code, notes, and snippets.

@xv
Last active June 15, 2022 19:57
Show Gist options
  • Save xv/336342b6724a4e87bd9ab6822f7c938d to your computer and use it in GitHub Desktop.
Save xv/336342b6724a4e87bd9ab6822f7c938d to your computer and use it in GitHub Desktop.
PowerShell script utilizing .NET WinForms to create a GUI with a live clock label.
Add-Type -AssemblyName System.Windows.Forms
$Win32_ShowWindow = @'
[DllImport("user32.dll")]
public static extern bool ShowWindow(int handle, int nCmdShow);
'@
$User32 = Add-Type -MemberDefinition $Win32_ShowWindow `
-Name 'User32' -Namespace 'Win32' -PassThru
# Minimize the PS console window
$SW_SHOWMINIMIZED = 2
$User32::ShowWindow(
(Get-Process -ID $PID).MainWindowHandle,
$SW_SHOWMINIMIZED
)
# Clock Form
$Frm_Clock = New-Object system.Windows.Forms.Form
$Frm_Clock.Size = "365, 100"
$Frm_Clock.text = "PSClock"
$Frm_Clock.BackColor= "#ffffff"
$Frm_Clock.MaximizeBox = 0
$Frm_Clock.MinimizeBox = 1
$Frm_Clock.ShowIcon = 0
$Frm_Clock.FormBorderStyle = "FixedSingle"
$Frm_Clock.StartPosition = "CenterScreen"
$Frm_Clock.TopMost = 1
# Clock Label
$Lbl_Clock = New-Object System.Windows.Forms.Label
$Lbl_Clock.AutoSize = 0
$Lbl_Clock.Dock = "Fill"
$Lbl_Clock.Font = New-Object System.Drawing.Font("Segoe UI", 24)
$Lbl_Clock.ForeColor = "Brown"
$Lbl_Clock.TextAlign = "MiddleCenter"
$Lbl_Clock.Text = "00/00/0000 00:00 XM"
Function UpdateClock()
{
$Lbl_Clock.Text = Get-Date -Format "MM/dd/yyyy hh:mm tt"
}
# Timer to update clock
$Tmr_Update = New-Object System.Windows.Forms.Timer
$Tmr_Update.Interval = 100
$Tmr_Update.Add_Tick({ UpdateClock })
Function StartTimer()
{
$Tmr_Update.Enabled = 1
$Tmr_Update.Start()
}
Function StopTimer()
{
$Tmr_Update.Stop()
$Tmr_Update.Enabled = 0
}
StartTimer
$Frm_Clock.Controls.Add($Lbl_Clock)
$Frm_Clock.ShowDialog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment