A PowerShell script to calculate exercise training heart rate zones. It is intended for use by non-technical users. For this reason the design criteria were selected with the needs of consumers in mind. The script demonstrates how to use Windows Forms in PowerShell. To support the most clients, the script is coded for PowerShell version 1. Where possible, the script presents a GUI interactive interface. However, the script also supports clients where Windows Forms are not allowed, or where the user requests a command line interface. The script should run on platforms, such as Windows 8.1 RT, where PowerShell is constrained.

The training heart rate zones are output on the form and at the command line. The output can be redirected to a text file. The user can select from several formulas to estimate the maximum heart rate based on their sex and age. Besides providing useful results, the script also demonstrates the following:

  1. How to specify script parameters.
  2. Techniques for validating the input values.
  3. How to prompt the user for valid input.
  4. Providing help for the script.
  5. How to test if PowerShell is constrained.
  6. How to use Windows Forms and various controls in PowerShell.
  7. How to make the GUI interactive, using controls that implement code.
  8. How to make various controls only visible when appropriate.
  9. How to recognize when the Enter or Escape keys are pressed and run code on these events.

The training heart rate calculations are based on the Karvonen method, as described in the book "Serious Training for Serious Athletes", by Rob Sleamaker, Leisure Press, 1989, pages 58-69. The formulas for estimating the maximum heart rate are based mostly on the Wikipedia page for Heart Rate (as of 1/21/2022):
https://en.wikipedia.org/wiki/Heart-rate

The script incorporates the following features:

  1. Functions are used in the script so that formulas can be modified if desired, perhaps based on more recent information.
  2. Variables are defined for validating input values, so they can be modified if desired. The values only need to be changed in one place, and all code and output are adjusted for the new limits.
  3. Many comments are included in the code to help understand the logic and explain the reasoning.

When the Windows form is shown, only code on the form can be run. It is important to have all necessary code defined in the controls, which run on events, such as button clicks. However, the code can call functions that have been defined elsewhere in the script. But a lot of testing is necessary to make sure the values necessary are passed to the functions. If necessary, variables that are assigned values in these functions must be defined to have Script or Global scope.

The parameters that can be passed to the script are:

Parameter Acceptable Values Description
-RestingHR Integer between 28 and 115 Resting heart rate in beats per minute. If no value is provided you will be prompted for a value.
-MaxHR Integer between 135 and 230 Maximum heart rate in beats per minute. If no value is provided an estimate is calculated based on your sex and age.
-Age integer between 5 and 105 Your age in years. Only needed if you do not provide a maximum heart rate, or you use the -Estimate parameter.
-Formula Oakland1, Oakland2, Haskell, Cooper, Tanaka, Gellish, Robergs, or Nes. The formula to use to estimate the maximum heart rate (if needed). The default is Oakland1.
-Estimate Switch to calculate an estimated maximum heart rate. You are prompted for age, or you can use the -Age parameter.
-NoGUI Switch requesting that the GUI form not be used. Data is input and all results are displayed at the command line.
-Black Switch to output results without using the Write-Host cmdlet, so the output can be redirected to a file.
-Help Switch to output help information.

Note: The first one (or several) letters of each parameter can be used as aliases. In addition, the parameters -RestingHR, -MaxHR, -Age, and -Formula can be listed in order without the parameter names. See the first example below.

Some usage Examples:


Calculate heart rate zones based on resting HR of 52 and maximum HR of 180.

.\HRZones.ps1 52 180


Same as the previous, but using parameters.

.\HRZones.ps1 -MaxHR 180 -RestingHR 52


Calculate zones based on resting HR of 55 and estimated maximum using the Cooper formula and a 40 year old. The 180 maximum HR will not be used.

.\HRZones.ps1 55 180 40 Cooper -est


Calculate heart rate zones based on a resting HR of 51 and an estimated maximum. The maximum heart rate will be calculated based on a 39 year old.

.\HRZones.ps1 -Estimate -Age 39 -RestingHR 51


Same as the previous, but no GUI is selected.

.\HRZones.ps1 -r 51 -a 39 -e -n


Specify a resting heart rate of 52, but be prompted for either maximum heart rate or age.

.\HRZones.ps1 -R 52


To be prompted for resting heart rate and either maximum heart rate or age.

.\HRZones.ps1


A batch file can be used to launch the script and pass up to 9 arguments. This can help non-technical people use the script without the need to know how to launch PowerShell. The batch file would be similar to below (named HRZones.bat):

@echo off
PowerShell -Command "& {.\HRZones.ps1 %1 %2 %3 %4 %5 %6 %7 %8 %9}"

This syntax is supported in PowerShell V1 and later versions. Only a maximum of 9 arguments (parameters and values) can be passed to the script. For example, the script can be launched with the following at a command prompt, if the user is in the directory where HRZones.bat is saved.

HRZones -r 52 -est -a 40 -f Cooper

The following image shows the form and all of the controls on the form. Note that there is no situation where all of these controls would be visible at once, so the code was modified to show them all. Also, the red arrows and text are shown even though the values are correct. Normally, the red only appears if a value is out of range or missing.

Update, February, 2022:
Major revisions have been made to the referenced Wikipedia article since the last update to this script in 2016. Some of the formulas used to estimate maximum heart rate have been removed from the Wikipedia article, others have been added. The Haskell formula remains, but is described as not useful. And the latest studies indicate there is no evidence to justify different formulas for men and women.

The changes in the script for Version 2.0 include:

  1. Removal of sex as a parameter for the maximum heart rate formulas.
  2. A help feature for the values requested in each textbox has been added.
  3. Help has been added for each of the maximum heart rate formulas.
  4. Intensity levels have been added to the heart rate training zones. These correspond to percentages of maximum VO2, a measure of aerobic capacity.
  5. The image of the form, showing all of the controls, has been updated to show the new formulas and controls.
  6. A button has been added to call up a form with a table that compares the maximum heart rates calculated from each of the formulas for many ages.
  7. Some wording has been updated.

HRZones.txt <<-- Click here to view or download the program