I'm trying to execute a PowerShell command using Mathematica. Someone has made it before?

I tested my command direct in PowerShell, and it worked nice, but no idea on how can I make Mathematica to call it. Here is a related answer dealing with scripts.

Maybe the new RunProcess can be a nice new function to handler that, and I could better collect the prompted results.

Here is the command I want to execute:

git -C C:\somePath pull ssh://[email protected]/myaccount/myProject.git

A better toy command is welcome...

  • 1
    $\begingroup$ Maybe try as the 'toy' command ls, which works in the PowerShell, but not in cmd.exe. $\endgroup$ Feb 23, 2015 at 3:25
  • $\begingroup$ Would cygwin be o.k. too? $\endgroup$ Feb 23, 2015 at 8:07
  • $\begingroup$ Can you explain why you insist on PowerShell? Git could certainly be run from cmd.exe just as well. Not having made active use of PowerShell I think the outstanding feature of it (compared to cmd.exe or cygwin) is that commandlets can return (collections) of objects instead of just "text". If you'd want to take advantage of that I think NETLink would be the way to go, there are several easy to find recipes which show code on how to start a PowerShell-engine and run commands in it and I think it shouldn't be difficult to make these working from Mathematica... $\endgroup$ Feb 23, 2015 at 8:36
  • $\begingroup$ Well... you can launch a regular BAT file that executes the PowerShell script. But this looks overkill for what you are trying to do. blog.danskingdom.com/… $\endgroup$ Feb 23, 2015 at 9:10
  • 1
    $\begingroup$ @Murta: I think the best solution for your actual problem would be to invest in your ssh client config. On a windows client using the putty/pageant combination does work best for me. You would just have to configure git so that it makes use of a ssh-client which asks peageant for the key. I have such a config working but use mercurial instead of git. But I can hardly imagine that git wouldn't let you configure it so that it can also be called from cmd.exe. You probably want to ask that on a git specific forum... $\endgroup$ Feb 23, 2015 at 16:55

3 Answers 3


Well, not a PowerShell use, but an example of the new V10 function RunProcess.

Using this link, I did this batch to add my private key in the repository ever time windows start:

for /f "tokens=1,2 delims==; " %%i in ('call ssh-agent') do (
if "echo" neq "%%i" (
set %%i=%%j
setx %%i %%j 
cd c:/keyPath
ssh-add myKey

Now I can update my git using RunProcess as:

"sh --login -i -c \"git -C C:/path/local/repository pull ssh://[email protected]/myaccount/myrep.git\"

With RunProcess you can have better control over system output using it secont argument.


You can use the new RunProcess as you have already noticed:

RunProcess[{"powershell", "Get-Help"}, "StandardOutput"]

Or you can stick to the old fashion (which is basically invoke powershell from cmd):

RunInCmd[cmd_] := 
        FromCharacterCode[ReadList["!" <> cmd, Byte],
                "CP936" (*change it to whatever codepage you want*)
          ] // StringSplit[#1, "\n"] &

RunInCmd["powershell Get-Help"]
  • 2
    $\begingroup$ I cannot get your RunProcess solution to work. It just hangs in there without producing output or errors. I even provided the full path but it still does nothing. The RunInCmd works well. (Windows 7, Mathematica 10.2) $\endgroup$ Oct 21, 2015 at 15:00
  • $\begingroup$ @GustavoDelfino Hmm... I guess powershell didn't exit, and mma is waiting forever. $\endgroup$
    – Silvia
    Oct 21, 2015 at 15:04
  • $\begingroup$ I observe the same as @GustavoDelfino with Mathematica 12.0 on Win7 x64: RunProcess solution just hangs while RunInCmd quickly returns the output. $\endgroup$ Aug 25, 2019 at 16:14
  • $\begingroup$ @AlexeyPopkov I don't have access to win7, but I just tried again on win10 with mma 12.0, it works fine. Maybe there is something different from the OS sides. $\endgroup$
    – Silvia
    Aug 26, 2019 at 7:56

This is another way of doing it:

Import["!powershell.exe \"Get-Help\"", "TEXT"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.