# Running PowerShell commands in Mathematica

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://git@github.com/myaccount/myProject.git


A better toy command is welcome...

• Maybe try as the 'toy' command ls, which works in the PowerShell, but not in cmd.exe. – 2012rcampion Feb 23 '15 at 3:25
• Would cygwin be o.k. too? – Rolf Mertig Feb 23 '15 at 8:07
• 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... – Albert Retey Feb 23 '15 at 8:36
• 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/… – Gustavo Delfino Feb 23 '15 at 9:10
• @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... – Albert Retey Feb 23 '15 at 16:55

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:

@ECHO OFF
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


Now I can update my git using RunProcess as:

RunProcess["CMD",All,
"sh --login -i -c \"git -C C:/path/local/repository pull ssh://git@github.com/myaccount/myrep.git\"
exit
"]


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_] :=
"CP936" (*change it to whatever codepage you want*)
] // StringSplit[#1, "\n"] &

RunInCmd["powershell Get-Help"]

• 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) – Gustavo Delfino Oct 21 '15 at 15:00
• @GustavoDelfino Hmm... I guess powershell didn't exit, and mma is waiting forever. – Silvia Oct 21 '15 at 15:04
• I observe the same as @GustavoDelfino with Mathematica 12.0 on Win7 x64: RunProcess solution just hangs while RunInCmd quickly returns the output. – Alexey Popkov Aug 25 '19 at 16:14
• @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. – Silvia Aug 26 '19 at 7:56

This is another way of doing it:

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