Created
January 23, 2026 04:19
-
-
Save anomixer/a2d601fe1bfda18b81d36d50390f7bd7 to your computer and use it in GitHub Desktop.
powershell stopwatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Add-Type -AssemblyName System.Windows.Forms | |
| # Create a new form | |
| $form = New-Object System.Windows.Forms.Form | |
| $form.Text = "Stopwatch" # Form title | |
| $form.Size = New-Object System.Drawing.Size(220, 130) # Set a smaller height for the form | |
| $form.StartPosition = "CenterScreen" # Center the form on the screen | |
| # Create a label to display time | |
| $label = New-Object System.Windows.Forms.Label | |
| $label.Size = New-Object System.Drawing.Size(200, 40) # Set the size of the label | |
| $label.Location = New-Object System.Drawing.Point(0, 10) # Adjusted position to move it a bit further left | |
| $label.Font = New-Object System.Drawing.Font("Arial", 24) # Set the font size | |
| $label.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter # Center the text vertically and horizontally | |
| $label.Text = "00:00.00" # Initial display time | |
| $form.Controls.Add($label) # Add the label to the form | |
| # Create a stopwatch object | |
| $stopwatch = [System.Diagnostics.Stopwatch]::new() # Create a stopwatch object | |
| $timer = New-Object System.Windows.Forms.Timer | |
| $timer.Interval = 10 # Update every 10 milliseconds | |
| # Timer Tick event | |
| $timer.Add_Tick({ | |
| if ($stopwatch.IsRunning) { # If the stopwatch is running | |
| $elapsed = $stopwatch.Elapsed # Get the elapsed time | |
| # Update the displayed time, ensuring milliseconds have two digits | |
| $label.Text = "{0:00}:{1:00}.{2:00}" -f $elapsed.Minutes, $elapsed.Seconds, [math]::Floor($elapsed.Milliseconds / 10) | |
| } | |
| }) | |
| # Create Start button | |
| $startButton = New-Object System.Windows.Forms.Button | |
| $startButton.Location = New-Object System.Drawing.Point(10, 60) # Position button closer to the clock | |
| $startButton.Size = New-Object System.Drawing.Size(50, 25) # Set the size of the button | |
| $startButton.Text = "Start" # Button text | |
| $startButton.Add_Click({ | |
| if (-not $stopwatch.IsRunning) { # If the stopwatch is not running | |
| $stopwatch.Start() # Start the stopwatch | |
| $timer.Start() # Start the timer | |
| } | |
| }) | |
| $form.Controls.Add($startButton) # Add the Start button to the form | |
| # Create Stop button | |
| $stopButton = New-Object System.Windows.Forms.Button | |
| $stopButton.Location = New-Object System.Drawing.Point(70, 60) # Position button next to Start button | |
| $stopButton.Size = New-Object System.Drawing.Size(50, 25) # Set the size of the button | |
| $stopButton.Text = "Stop" # Button text | |
| $stopButton.Add_Click({ | |
| if ($stopwatch.IsRunning) { # If the stopwatch is running | |
| $stopwatch.Stop() # Stop the stopwatch | |
| $timer.Stop() # Stop the timer | |
| } | |
| }) | |
| $form.Controls.Add($stopButton) # Add the Stop button to the form | |
| # Create Reset button | |
| $resetButton = New-Object System.Windows.Forms.Button | |
| $resetButton.Location = New-Object System.Drawing.Point(130, 60) # Position button next to Stop button | |
| $resetButton.Size = New-Object System.Drawing.Size(50, 25) # Set the size of the button | |
| $resetButton.Text = "Reset" # Button text | |
| $resetButton.Add_Click({ | |
| $stopwatch.Reset() # Reset the stopwatch | |
| $label.Text = "00:00.00" # Reset the displayed time | |
| }) | |
| $form.Controls.Add($resetButton) # Add the Reset button to the form | |
| # Show the form | |
| $form.Add_Shown({$form.Activate()}) # Activate the form | |
| [void]$form.ShowDialog() # Show the form |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment