• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
jeremydeanonline.com

jeremydeanonline.com

computer nerd and dad in the coastal bend

  • Home
  • Pay Me
  • Contact
  • Blog
  • Tech

powershell scripting

powershell rebooter

February 4, 2017 by jeremy Leave a Comment

This is a quick and simple script I came up with to reboot a PC with a warning and a timer. I’ve always wanted to write something like this to force people to reboot their computers, but of course the problem is always “what if they’re in the middle of something when the rebooter kicks off?”. So I think this should work nicely. Although I’ve written batch files for years, I am a newbie to Powershell and certainly to the Powershell GUI interface. I plan to polish this up, but this works and serves its intended purpose (at least on the PCs I’ve tried it on). I cannot take credit for all of the code, as noted in the comments on the first lines. I found something similar and tweaked it to meet my needs – I think that’s a given this day and age, but I want to give credit where it is due. Here’s a screenshot:

And here is the code (thanks to hilite.me for making it so easy to highlight this properly in html):

#Displays a message warning user that PC will reboot in a specified number of seconds.
#Found original code at https://stackoverflow.com/questions/40575025/powershell-close-form-after-time-period 
#by esperento57 (https://stackoverflow.com/users/3735690/esperento57)
#I edited the code to suit my needs
#RUN set-executionpolicy unrestricted
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

Function ClearAndClose() 
{
$Timer.Stop(); 
$Form.Close(); 
$Form.Dispose();
$Timer.Dispose();
$Label.Dispose();
$Button.Dispose();

}

Function Button_Click()
{
ClearAndClose
}

Function Timer_Tick()
{

$Label.Text = "PC will reboot automatically in $Script:CountDown seconds unless you press the big button below. This is an automated task set to run once every twenty-four hours. PCs in our environment need to be rebooted regularly to ensure optimal performance."
 --$Script:CountDown
 if ($Script:CountDown -lt 0)
 {
	ClearAndClose
 }
}


$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Rebooter"
$Form.Size = New-Object System.Drawing.Size(530,330)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True

$Label = New-Object System.Windows.Forms.Label
$Label.Size = New-Object System.Drawing.Size(510,100)
$Label.Location = New-Object System.Drawing.Size(100,500)
$Label.location = new-object system.drawing.point(6,6)


$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(120,115)
$Button.Size = New-Object System.Drawing.Size(260,120)
$Button.Text = "Click Here To CANCEL REBOOT"
$Button.DialogResult=[System.Windows.Forms.DialogResult]::OK

$Timer = New-Object System.Windows.Forms.Timer
$Timer.Interval = 1000

$Form.Controls.Add($Label)
$Form.Controls.Add($Button)

$Script:CountDown = 10

$Button.Add_Click({Button_Click})
$Timer.Add_Tick({ Timer_Tick})


$Timer.Start()
$Form.ShowDialog()
#Write-Host Countdown: $Script:CountDown
if ($Script:CountDown -lt 0){Restart-Computer}

Filed Under: tech Tagged With: powershell, powershell scripting, rebooter, windows scripting

Primary Sidebar

posts

  • my life (10)
  • tech (3)

Copyright © 2021 · Powered by Genesis Framework · WordPress

  • Facebook
  • LinkedIn
  • Email