Create a focus time countdown clock using PowerShell

Sometimes you really need to finish something and notifications from your running programs keep taking away your focus… You can try using the script in this blog post to keep focused on your task, when the count-down timer is done your programs will be available again. Hope this helps 🙂

What is focus time and why should I use it?

Focus time is a dedicated block of time that you set aside for productive work on a task or project, without interruption. This time management strategy allows you to prioritize the important work that requires your undivided attention so you can actually produce high-quality results. Regardless of whether you’re a manager or maker, or need to spend time writing, coding, designing, or researching, making time on your calendar for heads-down work will not only help you get it done but done faster with less room for interruptions.

Starting the script

The Start-FocusTime function can be started from a PowerShell prompt together with the -Minutes parameter in which you specify the number of minutes it should wait before starting the programs again that were closed. Inside the script you can specify the program names that should be closed:

    #Get a list from your Task Manager for the correct names, look for them in the Details pane and add them without the extension
    #For example Outlook instead of Outlook.exe
    $ProgramsToKill = @(
        "Outlook",
        "Spotify",
        "Teams"
    )

You can make this list as long as you need, be aware that it will kill these programs after you press Enter when the script is started, and make sure you save any open documents to avoid losing data.

When started, (I used Start-FocusTime -Minutes 60 in this example) it will show output like this:

Starting Focus Time for 60 minutes, please close and save open document (if any) in the following programs:
Outlook, Spotify, Teams
Press Enter to continue: 

After pressing Enter, it will close the mentioned programs and start the countdown:

Closing programs if active...
Closing Outlook
Closing Spotify
Closing Teams
Starting focus time for 60 minutes
 \  0D 00h 59m 48s 

When the focus timer is done, it will start the programs again and stop:

 *  Countdown finished, restarting programs...
Starting Outlook
Starting Spotify
Starting Teams

Some programs can’t be started from the PowerShell prompt because they are not in the system or user path, it will show an error like the one below and you will have to start the program manually:

WARNING: Could not start Teams, please start it manually...

The script

The function is listed below, save it to a location and add it to your PowerShell profile so that you can start it anytime you want to. Follow these steps to add it to your profile, I used c:\data as the location where the file is saved and adjust it to your preference:

- notepad $profile
- Add . c:\data\Start-FocusTime.ps1
- Save/Close and start new PowerShell session, start-FocusTime should be available in it now
function Start-FocusTime {
    param (
        [Parameter(Mandatory = $true, HelpMessage = "Enter the amount of minutes you want to focus in")][string]$Minutes

    )

    #Get a list from your Task Manager for the correct names, look for them in the Details pane and add them without the extension
    #For example Outlook instead of Outlook.exe
    $ProgramsToKill = @(
        "Outlook",
        "Spotify",
        "Teams"
    )
    
    #Close all programs in the $ProgramsToKill variable
    Write-Host ("Starting Focus Time for {0} minutes, please close and save open document (if any) in the following programs:" -f $Minutes) -ForegroundColor Green
    $ProgramsToKill -join ', ' | Sort-Object
    Read-Host -Prompt "Press Enter to continue" 
    Write-Host "Closing programs if active..." -ForegroundColor Green
    foreach ($program in $ProgramsToKill) {
        if (Get-Process $program -ErrorAction SilentlyContinue) {
            try {
                Get-Process $Program | Stop-Process -Force:$true -Confirm:$false -ErrorAction Stop
                Write-Host ("Closing {0}" -f $program) -ForegroundColor Green
            }
            catch {
                Write-Warning ("Could not close {0}, please close it manually..." -f $program)
            }
        }
    }

    #Start countdown, countdown to zero and restart programs again
    #Used countdown procedure from https://www.powershellgallery.com/packages/start-countdowntimer/1.0/Content/Start-CountdownTimer.psm1
    $t = New-TimeSpan -Minutes $Minutes
    $origpos = $host.UI.RawUI.CursorPosition
    $spinner = @('|', '/', '-', '\')
    $spinnerPos = 0
    $remain = $t
    $d = ( get-date) + $t
    [int]$TickLength = 1
    $remain = ($d - (get-date))
    Write-Host ("Starting focus time for {0} minutes" -f $Minutes) -ForegroundColor Green
    while ($remain.TotalSeconds -gt 0) {
        Write-Host (" {0} " -f $spinner[$spinnerPos % 4]) -ForegroundColor Green -NoNewline
        write-host (" {0}D {1:d2}h {2:d2}m {3:d2}s " -f $remain.Days, $remain.Hours, $remain.Minutes, $remain.Seconds) -NoNewline
        $host.UI.RawUI.CursorPosition = $origpos
        $spinnerPos += 1
        Start-Sleep -seconds $TickLength
        $remain = ($d - (get-date))
    }
    $host.UI.RawUI.CursorPosition = $origpos
    Write-Host " * "  -ForegroundColor Green -NoNewline
    write-host " Countdown finished, restarting programs..." -ForegroundColor Green
    
    foreach ($program in $ProgramsToKill) {
        try {
            Write-Host ("Starting {0}" -f $program) -ForegroundColor Green
            Start-Process $program
        }
        catch {
            Write-Warning ("Could not start {0}, please start it manually..." -f $program)
        }
    }
}

Download the script(s) from GitHub here

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.