title: Invoke MSYS2 Shell from Command Prompt or PowerShell
body: <p>Starting from Command Prompt or PowerShell (either is fine; I can figure out how to do the other given one), how can I invoke the MSYS2 shell to run a command?</p>
  
  <h3>More specific issue</h3>
  
  <p>I'm including this information to avoid an XY problem, but I strongly believe this is best solved by the answer to the above (more general) question.</p>
  
  <p>In particular, I'm hoping to write a shell script to update the packages listed in <a href="http://sourceforge.net/p/msys2/wiki/MSYS2%20installation/" rel="noreferrer">Part III, Step 3 here</a>. So I need something like:</p>
  
  <pre><code>[invoke msys2 bash] -c "pacman -Sy --needed msys2-runtime"
  </code></pre>
  
  <p>Calling the bash executable directly doesn't work:</p>
  
  <pre><code>C:\&gt;C:\msys64\usr\bin\bash.exe -c "pacman -Sy --needed msys2-runtime"
  /usr/bin/bash: pacman: command not found
  </code></pre>
  
  <p>I assume it's an environment problem. Changing <code>PATH</code> to include the path to MSYS2's bin directory doesn't fix the problem either. When it tries to install a package, bad things happens (nano is just an example):</p>
  
  <pre><code>C:\&gt;bash -c "pacman -Sy --needed --noconfirm nano"
  :: Synchronizing package databases...
   mingw32 is up to date
   mingw64 is up to date
   msys is up to date
  resolving dependencies...
  looking for conflicting packages...
  
  Packages (1) nano-2.3.6-2
  
  Total Installed Size:  1.81 MiB
  Net Upgrade Size:      0.17 MiB
  
  :: Proceed with installation? [Y/n]
  (1/1) checking keys in keyring                                                                   [########################################################] 100%
  (1/1) checking package integrity                                                                 [########################################################] 100%
  (1/1) loading package files                                                                      [########################################################] 100%
  (1/1) checking for file conflicts                                                                [########################################################] 100%
  (1/1) checking available disk space                                                              [########################################################] 100%
  (1/1) upgrading nano                                                                             [########################################################] 100%
  error: command (/usr/bin/bash /usr/bin/bash -c . /tmp/alpm_shqjUE/.INSTALL; post_upgrade 2.3.6-2 2.3.2-1 ) failed to execute correctly
  </code></pre>
  
  <p>Trying to invoke the batch file (C:\msys64\msys2_shell.bat) opens a new shell in a new window, which leaves me not knowing whether it failed or succeeded.</p>
  
  <p>So how can I get the shell into MSYS2's normal state?</p>