2

I'm trying to execute a ps1 file in my python script.

proc = subprocess.Popen(["powershell.exe", 'F:/FOLDER/FOLDER OK/myfile.ps1'])

It stopped at F:/FOLDER/FOLDER so I added additional quotes like this:

proc = subprocess.Popen(["powershell.exe", '"F:/FOLDER/FOLDER OK/myfile.ps1"'])

No error now, but it didn't execute the script either... Any idea?

6
  • Possible duplicate of How to run a powershell script with white spaces in path from command line? Commented Mar 5, 2018 at 12:55
  • windows sucks so much sometimes. Have you tried the following workaround: proc = subprocess.Popen(["powershell.exe", 'myfile.ps1'],cwd='F:/FOLDER/FOLDER OK') ? Commented Mar 5, 2018 at 12:59
  • 1
    @Mark - definitely relevant, but not a duplicate. Commented Mar 5, 2018 at 12:59
  • @Jean-FrançoisFabre didn't work :/ no error nothing, it just didn't execute the file Commented Mar 5, 2018 at 13:03
  • 2
    @JeffZeitlin Answer is there. Use -File and it will work. This isn't a Python-specific issue or anything from what it seems. Commented Mar 5, 2018 at 13:03

1 Answer 1

3

powershell.exe does not assume that a parameter is automatically a file to execute; you need to identify all parameters. Use the -File switch

proc = subprocess.Popen(["powershell.exe", "-File", r"F:\FOLDER\FOLDER OK\myfile.ps1"])

The r in r"..." (for raw) turns off escape-sequence processing so that \ can be used without escaping.

See PowerShell Command Line Help at Microsoft Docs.

PowerShell[.exe]
       [-Command { - | <script-block> [-args <arg-array>]
                     | <string> [<CommandParameters>] } ]
       [-EncodedCommand <Base64EncodedCommand>]
       [-ExecutionPolicy <ExecutionPolicy>]
       [-File <FilePath> [<Args>]]
       [-InputFormat {Text | XML}] 
       [-Mta]
       [-NoExit]
       [-NoLogo]
       [-NonInteractive] 
       [-NoProfile] 
       [-OutputFormat {Text | XML}] 
       [-PSConsoleFile <FilePath> | -Version <PowerShell version>]
       [-Sta]
       [-WindowStyle <style>]
Sign up to request clarification or add additional context in comments.

1 Comment

As an aside: PowerShell Core now defaults to -File, (thought it never hurts to be explicit; the change was necessary to support Unix shebang lines).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.