Powershell Git Hook Exit Code

I have the following in my .git/hooks/pre-commit file

exec c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -Command " Get-Location | % { '$_\pre-commit-hook.ps1'} | % { & $_ }"

This successfully executes the code in the pre-commit-hook.ps1 file in the same directory, but does not capture the exit code. According to tldp.org the last exit code will be returned if only exit is specified. Git hooks will fail if the exit code is non-zero, but even though my powershell script returns a status code of 1, it always succeeds. What can I do to capture the exit code from the powershell script so the hook will function correctly?

  • How to display a specific user's commits in svn log?
  • How do I use Notepad++ Compare as the diff tool in git bash/command line?
  • How can I set up autocompletion for Git commands?
  • In Git for Windows Bash, how to install “get-apt” and “sudo” and all those basic commands?
  • What is the difference between --parameter and -parameter?
  • How can you access an environment variable that has a space in its name in bash?
  • git-crypt cleartext protection?
  • Git/gitosis: How to check validity of user name and email?
  • Pushing a Git source tree, not just a repository
  • Why various git commands are available only locally?
  • What should be included in a windows phone 7 gitignore file?
  • Bitbucket not showing changes in themes directory
  • One Solution collect form web for “Powershell Git Hook Exit Code”

    Keep the invocation of the ps1 script simple and you should have it working. The following works for me:

    exec powershell.exe -ExecutionPolicy RemoteSigned -File '.\.git\hooks\pre-commit-hook.ps1'

    The ps1 script just had an exit 1 and the commit did not happen.

    When you are doing stuff like -command, Powershell is not known to work properly and you might have to do something like -command {& .\test.ps1; exit $lastexitcode}

    Git Baby is a git and github fan, let's start git clone.