title: Get the name of the current git branch with PowerShell
body: <p>I would like to be able to do git commands from a PowerShell script which is not located in the same folder as my git repo.</p>
  
  <p>First I want to check the current branch, and if it's not master try to checkout master and pull master.</p>
  
  <p>What I've done so far:</p>
  
  <pre class="lang-cs prettyprint-override"><code>function Get-ScriptDirectory {
      Split-Path $script:MyInvocation.MyCommand.Path
  }
  
  $currentPath = Get-ScriptDirectory
  [string]$Path_GIT = "C:\Program Files\Git\git-cmd.exe"
  $gitRepo = $currentPath + "\..\"
  $nameCurrentBranch = &amp; $Path_GIT git -C "$gitRepo" rev-parse --abbrev-ref HEAD
  </code></pre>
  
  <p>From the documentation <a href="https://git-scm.com/docs/git" rel="noreferrer">here</a> and the answers to <a href="https://stackoverflow.com/questions/6245570/how-to-get-the-current-branch-name-in-git">this question</a>.</p>
  
  <p><code>$gitRepo</code> contains the path of the folder containing the git repo.</p>
  
  <p>I get the error:</p>
  
  <pre>git-cmd.exe : fatal: Cannot change to 'C:\Users\MyName\Documents\Projects\MyProject\
  Batchs\.." rev-parse --abbrev-ref HEAD': Invalid argument
  At C:\Users\MyName\Documents\Projects\MyProject\Batchs\Publish.ps1:64 char:22
  + ... entBranch = & $Path_GIT git -C "$gitRepo" rev-parse --abbrev-ref HEAD ...
  +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (fatal: Cannot c...nvalid argument:String) [], RemoteException
      + FullyQualifiedErrorId : NativeCommandError</pre>
  
  <p><strong>EDIT:</strong> New problem after the proposition of @4c74356b41 (using <code>Join-Path</code>):
  I don't have any error a popup opens and close really fast and then my powershell script is stuck, as if it was waiting for the git-cmd.exe to close. But I can't see the windows of the cmd git and can't close it.</p>