How to configure the Powershell path environment variable on a Windows agent when using declarative pipeline

Article ID:360010917872
1 minute readKnowledge base

Issue

  • When running a Windows agent connected to a CloudBees Jenkins controller, you may want to set the Powershell path environment variable in order to execute specific commands on the agent.

Resolution

When setting the Powershell path from a declarative pipeline, one option is to call a batch file from the pipeline. Here is an example stage which calls a batch file:

stage('Run Tests') {
  steps {
    bat 'powershell -noexit "& "".\\run-tests.ps1"""'
  }
}

The Powershell path can then be set from within the Powershell script. An example of the contents of the run-tests.ps1 file are below:

$env:path = "$env:path;C:\selenium\geckodriver-v0.21.0-win64;C:\Program Files\nodejs"

npm install
node run-selenium-tests.js

In the above example, the $env:path is appended with a folder containing the selenium geckodriver as well as the path to a nodejs installation.