# How to output first 6 characters of the GIT\_COMMIT env\_var in Powershell?

**URL:** <https://community.jenkins.io/t/how-to-output-first-6-characters-of-the-git-commit-env-var-in-powershell/4714>\
**Category:** Ask a question\
**Created:** [November 25, 2022, 12:01pm UTC](https://community.jenkins.io/t/how-to-output-first-6-characters-of-the-git-commit-env-var-in-powershell/4714 "2022-11-25T12:01:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AxlInvk](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/axlinvk/32/582_2.png) [@AxlInvk](https://community.jenkins.io/u/AxlInvk)\
**Post date:** [November 25, 2022, 12:01pm UTC](https://community.jenkins.io/t/how-to-output-first-6-characters-of-the-git-commit-env-var-in-powershell/4714/1 "2022-11-25T12:01:51Z")

</div>

I am in the process of writing a Powershell script using the [Powershell plugin](https://plugins.jenkins.io/powershell/) 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.

---

<div class="post-metadata">

**Author:** ![slide\_o\_mix](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/slide_o_mix/32/20977_2.png) [@slide\_o\_mix](https://community.jenkins.io/u/slide_o_mix)\
**Post date:** [November 25, 2022, 2:24pm UTC](https://community.jenkins.io/t/how-to-output-first-6-characters-of-the-git-commit-env-var-in-powershell/4714/2 "2022-11-25T14:24:00Z")

</div>

The env variable is a .NET string type, so you can just use substring on it. [Substring() - PowerShell - SS64.com](https://ss64.com/ps/substring.html)

---

<div class="post-metadata">

**Author:** ![MarkEWaite](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/markewaite/32/20_2.png) [@MarkEWaite](https://community.jenkins.io/u/MarkEWaite)\
**Post date:** [November 25, 2022, 3:52pm UTC](https://community.jenkins.io/t/how-to-output-first-6-characters-of-the-git-commit-env-var-in-powershell/4714/3 "2022-11-25T15:52:53Z")

</div>

Or consider this answer from Stackoverflow:

> <https://stackoverflow.com/questions/27944302/substring-in-powershell-to-truncate-string-length>

---

<div class="post-metadata">

**Author:** ![AxlInvk](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/axlinvk/32/582_2.png) [@AxlInvk](https://community.jenkins.io/u/AxlInvk)\
**Post date:** [December 1, 2022, 3:31pm UTC](https://community.jenkins.io/t/how-to-output-first-6-characters-of-the-git-commit-env-var-in-powershell/4714/4 "2022-12-01T15:31:23Z")

</div>

Very useful, thank you @slide_o_mix!

---

<div class="post-metadata">

**Author:** ![AxlInvk](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/axlinvk/32/582_2.png) [@AxlInvk](https://community.jenkins.io/u/AxlInvk)\
**Post date:** [December 1, 2022, 3:31pm UTC](https://community.jenkins.io/t/how-to-output-first-6-characters-of-the-git-commit-env-var-in-powershell/4714/5 "2022-12-01T15:31:44Z")

</div>

Appreciate the help, thank you @MarkEWaite!
