0

I want to run a PowerShell command from cmd, because I need to install the Chocolatey package manager from my Python script. Like this:

import subprocess as sp
choco = sp.Popen(['powershell', 'Start-Process -verb runas powershell',
                  '"-noexit -command Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString(`https://chocolatey.org/install.ps1`)"'])

The problem is the URL must be in quotes and if I use URL ' or " this will unquote the URL.

I want to run the Chocolatey install command. I don’t want run a PowerShell script. I just want to run install Chocolatey and then I want to install some Chocolatey packages.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
heavyblack1
  • 72
  • 2
  • 9

1 Answers1

0
>>> import subprocess
>>> import sys
>>> p = subprocess.Popen(['powershell.exe', 'C:\\Temp\\test.ps1'], stdout=sys.stdout)
>>> Hello World
_
  • But I want run commannd not a script.I don't want use solution like save my command to file and run command from file and your method do not run script as admin. – heavyblack1 Nov 20 '19 at 22:07
  • 1
    Could you please add some explanation to your code. – nbk Nov 21 '19 at 00:48
  • Where does *"Hello World"* come from? It should be *[Hello, World!](https://en.wikipedia.org/wiki/%22Hello,_World!%22_program)*. – Peter Mortensen Sep 24 '21 at 16:11
  • An explanation would be in order. E.g., what is the idea/gist? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/58963973/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Sep 24 '21 at 16:11