KeyRoutines
StepProscript.powershell

Run a PowerShell script automatically from a routine

Run PowerShell runs PowerShell code you write in the step, or a .ps1 file. It needs KeyRoutines Pro and is for the jobs that no ready-made block covers yet.

KeyRoutines Pro is a one-time $29.99 upgrade for up to 2 PCs.

How it works

  • Write the script or pick a .ps1 file; it uses PowerShell 7 when installed and Windows PowerShell otherwise, unless you choose one.
  • Pass routine values under Values, read in the script as $env:KR_name, or as arguments, read as $args; nothing is ever pasted into the code itself.
  • It runs hidden, without your profile, with a time limit and a memory limit, and anything it started is stopped when it ends.
  • Outputs include the exit code, the output and error text, its lines, and parsed JSON when the script printed JSON with ConvertTo-Json.
  • Script steps stay off until Allow script steps is turned on under Settings ▸ Advanced, and a dry run skips this step.

Routines that use it

Free up disk space

Every … 7 days, Run PowerShell clears an old cache folder and Script result checks that it succeeded.

Report your battery health

A Hotkey runs a script that prints battery info as JSON, and Show banner shows the numbers it returned.

Imported routines stay safe

A routine shared by a friend that contains a script stays off until you review and approve it.

Settings

SettingWhat it does
SourceWrite the script here, or run a .ps1 file.
ScriptThe PowerShell code. Values from the routine aren't pasted in here: add them under Values and read them as $env:KR_name.
FileThe .ps1 file to run.
ValuesValues for the script, by name. The script reads each as $env:KR_<name> (for example $env:KR_folder).
ArgumentsArguments for the script, one per line. The script reads them as $args.
Working folderThe folder the script starts in. Empty: your user folder.
EditionadvancedWhich PowerShell runs it. Automatic uses PowerShell 7 when it's installed, else Windows PowerShell.
TimeoutThe script is stopped, with everything it started, after this long.
Fail on erroradvancedFail the step when the script ends with an exit code other than 0.
Memory mbThe most memory the script and everything it starts may use together.

Good to know

  • The script can't ask for input while it runs; anything waiting for typed input fails instead of hanging.
  • Long-running programs that should outlive the routine belong in Open app.

Questions

How do I run a PowerShell script at startup or on a schedule?
Use When KeyRoutines starts or At a time with a Run PowerShell step pointing at your .ps1 file. Turn on Allow script steps under Settings ▸ Advanced first.
Is it safe to run routines from other people that contain scripts?
An imported routine with a script stays off until you approve it, and the preview marks it clearly as running a script. Any later change to the script step turns it off again until you approve it.

Pairs well with

Build this routine in KeyRoutines

Free from the Microsoft Store for Windows 11 and 10. Pro is a one-time upgrade.