Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Powershell sleep function, with progress bar.
function Start-Sleep($seconds) {
$doneDT = (Get-Date).AddSeconds($seconds)
while($doneDT -gt (Get-Date)) {
$secondsLeft = $doneDT.Subtract((Get-Date)).TotalSeconds
$percent = ($seconds - $secondsLeft) / $seconds * 100
Write-Progress -Activity "Sleeping" -Status "Sleeping..." -SecondsRemaining $secondsLeft -PercentComplete $percent
Write-Progress -Activity "Sleeping" -Status "Sleeping..." -SecondsRemaining 0 -Completed

This comment has been minimized.

Copy link

@evoelker evoelker commented Oct 3, 2017

This was just what I was looking for. Made some changes so I could still use Start-Sleep and to give wait time in minutes and seconds.


This comment has been minimized.

Copy link

@J-Coc J-Coc commented Nov 29, 2018

I'm trying to use your function and get it to display in a GUI Form. The code above works just fine to display the progress bar while in ISE, but when I run my whole code it only shows in the Console. I'm wanting to have it actually pop open a GUI window within the boundary of my main form and display the bar, any thoughts how I could do that? I'm still working on my Poweshell GUI App like getting the $ChosenItem property text to display in label13 and a few more enhancements. Thank you for your help on this, here is my code below:


Load Assemblies

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
$net = New-Object -ComObject Wscript.Network

Display progress bar

<# #>
Function Start-Sleep($seconds)
$doneDT = (Get-Date).AddSeconds($seconds)
while($doneDT -gt (Get-Date))
$secondsLeft = $doneDT.Subtract((Get-Date)).TotalSeconds
$percent = ($seconds - $secondsLeft) / $seconds * 100
Write-Progress -Activity "Pausing" -Status "Pausing..." -SecondsRemaining $secondsLeft -PercentComplete $percent
Write-Progress -Activity "Pausing" -Status "Pausing..." -SecondsRemaining 0 -Completed

Display the Main Screen

Function Display-MainScreen

---------- Button Click Actions ------

Define Return Button Click Function

Function Return-MainScreen


Define OK Button Click Function

Function Display-UserInfo($ChosenItem)

Clear the window


Create Output

Add-Type -AssemblyName System.Windows.Forms

Get user Information

$Selecteduser = Get-ADUser -Identity $ChosenItem -Properties Name,SID,description,sAMAccountName,title,canonicalname
$Global:Name = $Selecteduser.Name
$Sam = $Selecteduser.sAMAccountName
$desc = $Selecteduser.description
$sid = $Selecteduser.sid
$Title = $Selecteduser.title
$OU = $Selecteduser.canonicalname
#$output = $sam

Create text

$Label.Text = 'Display Name: ' + $Name
$Label4.Text = 'AD Sam Name: ' + $Sam
$Label5.Text = 'Description: ' + $desc
$Label6.Text = 'Job Title: ' + $title
$Label7.Text = 'SID: ' + $sid
$Label8.Text = 'OU: ' + $OU

$Label9.Text = ''
$Label10.Text =''
$Label11.Text =''
$Label12.Text =''
$Label13.Text =''

Display Screen Contents

#If ($State -eq 'Off')
#If ($State -EQ 'Running')

------ Define All GUI Objects-------

Define Label - This is a text box object that will display user data

$Label = New-Object System.Windows.Forms.Label
$Label.AutoSize = $False
$Label.Location = new-object System.Drawing.Size(50,30)
$Label.Size = New-Object System.Drawing.Size(320,20)
$Label.ForeColor = "Black"
$label.BackColor = 'White'

Define Label2 - The Please Make a Selection Text

$Label2 = New-Object System.Windows.Forms.Label
$Label2.AutoSize = $True
$Label2.Location = new-object System.Drawing.Size(20,50)
$Label2.ForeColor = "DarkBlue"
$Label2.Text = "Please select an AD User to Term."

Define Label3 - The This is Your Selected AD User Text

$Label3 = New-Object System.Windows.Forms.Label
$Label3.AutoSize = $True
$Label3.Location = new-object System.Drawing.Size(50,10)
$Label3.ForeColor = "DarkBlue"
$Label3.Text = "Your selected AD User:"

Define Label4 - The This is the User label

$Label4 = New-Object System.Windows.Forms.Label
$Label4.AutoSize = $False
$Label4.Location = new-object System.Drawing.Size(50,50)
$Label4.Size = New-Object System.Drawing.Size(320,20)
$Label4.ForeColor = "Black"
$Label4.BackColor = 'White'

Define Label5 - The This is the description label

$Label5 = New-Object System.Windows.Forms.Label
$Label5.AutoSize = $False
$Label5.Location = new-object System.Drawing.Size(50,70)
$Label5.Size = New-Object System.Drawing.Size(320,20)
$Label5.ForeColor = "Black"
$Label5.BackColor = "White"

Define Label6 - The This is the SID label

$Label6 = New-Object System.Windows.Forms.Label
$Label6.AutoSize = $False
$Label6.Location = new-object System.Drawing.Size(50,90)
$Label6.Size = New-Object System.Drawing.Size(320,20)
$Label6.ForeColor = "Black"
$Label6.BackColor = 'White'

Define Label7 - The This is the Job Title label

$Label7 = New-Object System.Windows.Forms.Label
$Label7.AutoSize = $False
$Label7.Location = new-object System.Drawing.Size(50,110)
$Label7.Size = New-Object System.Drawing.Size(320,20)
$Label7.ForeColor = "Black"
$Label7.BackColor = 'White'

Define Label8 - The This is the ou label

$Label8 = New-Object System.Windows.Forms.Label
$Label8.AutoSize = $False
$Label8.Location = new-object System.Drawing.Size(50,130)
$Label8.Size = New-Object System.Drawing.Size(320,20)
$Label8.ForeColor = "Black"
$Label8.BackColor = 'White'

Define Label9 - This is the Disabled user text

$Label9 = New-Object System.Windows.Forms.Label
$Label9.Location = New-Object System.Drawing.Size(50,200)
$Label9.Size = New-Object System.Drawing.Size(320,20)
$Label9.ForeColor = "Black"
$Label9.BackColor = 'White'

Define Label10 - This is the AD Group changes text

$Label10 = New-Object System.Windows.Forms.Label
$Label10.Location = New-Object System.Drawing.Size(50,220)
$Label10.Size = New-Object System.Drawing.Size(320,20)
$Label10.ForeColor = "Black"
$Label10.BackColor = 'White'

Define Label11 - This is the AD password changes text

$Label11 = New-Object System.Windows.Forms.Label
$Label11.Location = New-Object System.Drawing.Size(50,240)
$Label11.Size = New-Object System.Drawing.Size(320,20)
$Label11.ForeColor = "Black"
$Label11.BackColor = 'White'

Define Label12 - This is the AD OU changes text

$Label12 = New-Object System.Windows.Forms.Label
$Label12.Location = New-Object System.Drawing.Size(50,260)
$Label12.Size = New-Object System.Drawing.Size(320,20)
$Label12.ForeColor = "Black"
$Label12.BackColor = 'White'

Define Label13 - This is the Termed text

$Label13 = New-Object System.Windows.Forms.Label
$Label13.Location = New-Object System.Drawing.Size(50,280)
$Label13.Size = New-Object System.Drawing.Size(320,50)
$Label13.ForeColor = "Black"
$Label13.BackColor = 'White'

Define List Box - This will display the Users that can be selected

$ListBox1 = New-Object System.Windows.Forms.ListBox
$ListBox1.Location = New-Object System.Drawing.Size(20,80)
$ListBox1.Size = New-Object System.Drawing.Size(260,20)
$ListBox1.Height = 80

This code populates the list box with User names

$ADusers = Get-ADUser -filter {(Enabled -eq $True)} -Searchscope 2 -Properties Name,SID,description,sAMAccountName,title,canonicalname | Sort-Object -Property sAMAccountName
ForEach ($name in $ADusers)
[void] $ListBox1.Items.Add($name.sAMAccountName)

Define Button 1 - This is the selection screen's OK button

$Button1 = new-object System.Windows.Forms.Button
$Button1.Location = new-object System.Drawing.Size(20,170)
$Button1.Size = new-object System.Drawing.Size(70,30)
$Button1.BackColor ="LightGray"
$Button1.Text = "OK"
$Button1.Add_Click({$ChosenItem=$ListBox1.SelectedItem;Display-UserInfo $ChosenItem})

Define Button 2 - This is the selection screen's Cancel button

$Button2 = New-Object System.Windows.Forms.Button
$Button2.Location = New-Object System.Drawing.Size(120,170)
$Button2.Size = New-Object System.Drawing.Size(70,30)
$Button2.BackColor ="LightGray"
$Button2.Text = "Cancel"

Define Button 3 - This is the Return to Main Screen button

$Button3 = New-Object System.Windows.Forms.Button
$Button3.Location = New-Object System.Drawing.Size(50,160)
$Button3.Size = New-Object System.Drawing.Size(150,30)
$Button3.BackColor ="LightGray"
$Button3.Text = "Return to Main Screen"

Define Button 4 - This button Runs the script to term selected user

$Button4 = New-Object System.Windows.Forms.Button
$Button4.Location = New-Object System.Drawing.Size(220,160)
$Button4.Size = New-Object System.Drawing.Size(150,30)
$Button4.BackColor ="LightGray"
$Button4.Text = "Term Selected User"

$newLine = [System.Environment]::NewLine

$WPFsamBox = $Selecteduser

#Set-ADUser -Identity $WPFsamBox -Enabled 0

$Label9.Text = 'Pausing for 10 seconds for User to be disabled'

Start-Sleep 2
#Displays in ISE when run, but not from GUI

$Label10.Text = 'Pausing for 10 seconds for AD Group Changes'

Start-Sleep 2
#Displays in ISE when run, but not from GUI

$Label11.Text ="Pausing for 10 seconds for AD Password Changes"

Start-Sleep 2
#Displays in ISE when run, but not from GUI

$Label12.Text ="Waiting for user to be moved to Pending Deletion OU"

Start-Sleep 2
#Displays in ISE when run, but not from GUI

$Label13.Text ="$WPFsamBox has been terminated"


-------- This is the end of the object definition section ------

-----Draw the empty form----

$Form = New-Object System.Windows.Forms.Form
$Form.width = 525
$Form.height = 380
$Form.BackColor = "lightblue"
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$Form.Text = "Disable Users"
$Form.maximumsize = New-Object System.Drawing.Size(525,380)
$Form.startposition = "centerscreen"
$Form.KeyPreview = $True

$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")

----Populate the form----



This comment has been minimized.

Copy link

@bakins929 bakins929 commented Feb 22, 2021

This is great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment