How to output first 6 characters of the GIT_COMMIT env_var in Powershell?

I am in the process of writing a Powershell script using the Powershell plugin that overwrites the version string in a text file using the following method – “Major.Minor.$ENV:BUILD_NUMBER.$ENV:GIT_COMMIT”.

When I call the $ENV:GIT_COMMIT environment variable, it outputs the entire 40-bit commit hash (example, 0123456789ABC…). I would like to use only the first 6 characters in the commit hash (e.g. 012345).

Any assistance in this matter is greatly appreciated!

Specs:

  • Windows 10 PC as a master device.
  • Jenkins LTS controller 2.361.2
  • Firefox browser.

The env variable is a .NET string type, so you can just use substring on it. Substring() - PowerShell - SS64.com

Or consider this answer from Stackoverflow:

Very useful, thank you @slide_o_mix!

1 Like

Appreciate the help, thank you @MarkEWaite!