Jenkins & PowerShell – Create Jenkins environment variables during a job run

In Jenkins there are a set of default jenkins environment variables that you can call from within a Powershell Build step. You can add a powershell script into a job’s build step with the used of the Jenkins Powershell plugin. The Powershell build step can then access a jenkins job’s build parameter, under the $env scope, that is::

 if ($env:JenkinsParameter -eq $null){
        "ERROR: Cannot find value for 'JenkinsParameter'"
        exit 1
 $JenkinsParameter = $env:JenkinsParameter

You can also access Jenkins environment variables in the same way, E.g. if you want to access the “Job_name” envrinment variable, then you do:

write-host $JOB_NAME

Also in your powershell script you can create environment variables that can be accessible by any latter powershell build steps. Also if you set the environment at the “machine” scope than these variables will be available by powershell build steps in different jenkins jobs. But that’s not good practice because you want your Jenkins jobs self contained.

However “powershell environment variables” are not accessible by any other types of build step (e.g. a python build step). You can only pass them from one powershell build step to any other successive powershell build steps that are within the same Job.

The only way round this issue to create a variable as “Jenkins environment variable” (aka a jenkins job parameter that gets set midway through the job’s run). This is possible with the help of the EnvInject plugin.

It is actually really easy to do. The way it works is that your powershell build step actually outputs all the variables to a file using the key=value syntax, e.g. here we created a file falled “”:

PS C:\> "message=HelloWorld" | Out-File -Encoding ASCII

Straight after the powershell build step we add a new “inject” build step:





Then do:




Finally you can append another powershell build step and call this new jenkins environment variable like this:


write-host "variables have been injected"
write-host "the message is $env:message"

Note: This, this technique of using envinject plugin isn’t limited to powershell, the equivalent should work with other languages e.g. with python and ruby.

Note: you can also pass the variables stored in to other jobs by triggering them from the current job. You can do because these types of build steps natively supports a properties file, e.g. :


here’s an example of a post build step triggering another jenkins job:


Hence this file has two possible uses, it creates new environment variables midway through current jobs (using the EnvInject) plugin, and it can be used as an input file for triggering other jenkins jobs.

Warning: You might have problems getting your powershell script working becuase of “second-hop”/remoting related issues. The way I have managed to fix this is by not using the jenkins powershell plugin. Instead I ran the powershell script inside an ordinary “execute windows batch command build step”:


Note, to do the above, you first need to save your powershell code into a .ps1 file and add it to your code repo, in order to make it available via your jenkins workspace.