How to use a Jenkins variable in my Powershell Script

I'm not familiar with Jenkins, but I believe BUILD_NUMBER is an environment variable.

To access it in PowerShell, use $env:BUILD_NUMBER

E.g. If using 7-zip

7z.exe a "$env:BUILD_NUMBER.zip" C:\Build\Path 

You can add arguments to your Script.ps1. Just use Param at the top of the script:

Param( $BuildNumber ) #must be first statement in script
# your current script goes here

then you can call the script passing BUILD_NUMBER as argument from Jenkins. Refer to this question for calling Powershell script with argument.


You could also use the Powershell Plugin, see here.

It allows you to enter PowerShell commands directly in a text box in Jenkins.

Then you can use the available environment variables (see docu of the plugin). The usage is a little cryptic:

(Get-Content ./Sources/SlmMachineControl/GUI/Project1.rc).replace("`"FileVersion`", `"1.0.0.0`"" ,"`"FileVersion`" `"2.3.$($env:BUILD_NUMBER).0`"") | Set-Content ./Sources/SlmMachineControl/GUI/Project1.rc

Also note the escaping of the double-quotes. Took me quite a while :)