r/PowerShell • • Feb 02 '23

Question Powershell script to shutdown a computer

Hi, I could use your help. I am trying to create a Powershell script that allows me to shutdown a computer after 10 minutes. Once I run the script I wanted a window to pop up telling the user that the computer would shut down in 10 minutes, and if he wanted to cancel. If he hit the cancel button the script would stop, if he didn't touch the computer after 10 minutes it would shutdown. Can you help me create a script like this? Thanks.

0 Upvotes

28 comments sorted by

View all comments

1

u/nawadsama Feb 03 '23
$main = New-Object System.Windows.Forms.Form
$main.ClientSize = '220,200'
$main.MinimizeBox = $false
$main.MaximizeBox = $false
$main.FormBorderStyle = [System.Windows.Forms.BorderStyle]::None
$main.Text = 'Tajm'
$main.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$label = New-Object System.Windows.Forms.Label
$label.Size = '190,30'
$label.Location = '10,10'
$label.Text = 'Ile minut?'
$font = New-Object System.Drawing.Font("Arial", 23)
$label.Font = $font
$textbox = New-Object System.Windows.Forms.TextBox
$textbox.Size = '50,100'
$textbox.Location = '10,70'
$button = New-Object System.Windows.Forms.Button
$button.Size = '50,50'
$button.Location = '10,120'
$button.Text = 'GO'
$button.Add_Click({
$timer.Start()
$textbox.Visible =$false
$button.Visible = $false
$label.Visible = $false
$script:1 = New-TimeSpan -minutes ([int]$textbox.Text)
$label2.Visible = $true
$label2.Text = $1.ToString("mm\:ss")
$button2.Location = [System.Drawing.Point]::new(10,100)
$button2.Size = [System.Drawing.Size]::new(200,90)
})
$timer = New-Object System.Windows.Forms.Timer
$timer.Enabled = $false
$timer.Interval = '1000'
$second = New-TimeSpan -Seconds 1
$timer.Add_Tick({
$script:1 = $1.Subtract($second)
$label2.Text = $1.ToString("mm\:ss")
if($script:1.TotalSeconds -le 0){$timer.Stop(); $timer.Dispose(); [System.Windows.Forms.Application]::SetSuspendState("Hibernate", $true, $false); $main.Close(); $main.Dispose()}
})
$label2 = New-Object System.Windows.Forms.Label
$label2.Size = '210,100'
$label2.Location = '10,10'
$label2.Text = $1
$label2.ForeColor = [System.Drawing.Color]::Crimson
$font = New-Object System.Drawing.Font("Arial", 23, [System.Drawing.FontStyle]::Underline)
$label2.Font = $font
$label2.TextAlign = 'MiddleCenter'
$label2.Visible = $false
$button2 = [System.Windows.Forms.Button]::new()
$button2.Size = [System.Drawing.Size]::new(50,50)
$button2.Location = [System.Drawing.Point]::new(60,120)
$button2.Text = 'Cancel'
$button2.Add_Click({
$timer.Stop()
$main.Close()
})
$main.Controls.Add($textbox)
$main.Controls.Add($button)
$main.Controls.Add($button2)
$main.Controls.Add($label)
$main.Controls.Add($label2)
$main.ShowDialog()

This is a part of a bigger script I use on a regular basis. You can tweak some parts and it should do what you want.

1

u/nawadsama Feb 03 '23
$main = New-Object System.Windows.Forms.Form

$main.ClientSize = '220,200' $main.MinimizeBox = $false $main.MaximizeBox = $false $main.FormBorderStyle = [System.Windows.Forms.BorderStyle]::None $main.Text = 'Tajm' $main.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen $timer = New-Object System.Windows.Forms.Timer $timer.Enabled = $false $timer.Interval = '1000' $second = New-TimeSpan -Seconds 1 $timer.Start() $1 = New-TimeSpan -minutes 10 $timer.Add_Tick({ $script:1 = $1.Subtract($second) $label2.Text = $1.ToString("mm:ss") if($script:1.TotalSeconds -le 0) {$timer.Stop() $timer.Dispose() Stop-Computer $main.Close() $main.Dispose()} }) $label2 = New-Object System.Windows.Forms.Label $label2.Size = '210,100' $label2.Location = '10,10' $label2.Text = $1 $label2.ForeColor = [System.Drawing.Color]::Crimson $font = New-Object System.Drawing.Font("Arial", 23, [System.Drawing.FontStyle]::Underline) $label2.Font = $font $label2.TextAlign = 'MiddleCenter' $label2.Visible = $true $button2 = [System.Windows.Forms.Button]::new() $button2.Size = [System.Drawing.Size]::new(50,50) $button2.Location = [System.Drawing.Point]::new(60,120) $button2.Text = 'Cancel' $button2.Add_Click({ $timer.Stop() $main.Close() }) $main.Controls.Add($button2) $main.Controls.Add($label2) $main.ShowDialog()

Like this