Running PowerShell scripts as git hooks Running PowerShell scripts as git hooks git git

Running PowerShell scripts as git hooks


You can embed PowerShell script directly inside the hook file. Here is an example of a pre-commit hook I've used:

#!/usr/bin/env pwsh# Verify user's Git config has appropriate email addressif ($env:GIT_AUTHOR_EMAIL -notmatch '@(non\.)?acme\.com$') {    Write-Warning "Your Git email address '$env:GIT_AUTHOR_EMAIL' is not configured correctly."    Write-Warning "It should end with '@acme.com' or '@non.acme.com'."    Write-Warning "Use the command: 'git config --global user.email <name@acme.com>' to set it correctly."    exit 1}exit 0

This example requires PowerShell Core but as a result it will run cross-platform (assuming this file has been chmod +x on Linux/macOS).


Rename pre-commit.sample to pre-commit in hooks folder.Then make pre-commit.ps1 powershell script file in same folder.

#!/bin/shc:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -File '.git\hooks\pre-commit.ps1'


From what I gather the only option due to Git's design here would be a bash script calling PowerShell. Unfortunate, but then again, Git didn't place any thought on non-Linux compatibility.