Home > Mobile >  Run Powershell code from Python - Pass variable
Run Powershell code from Python - Pass variable

Time:11-12

I want add a folder to the Quick Access in Windows via Python.
Python itself can not do that, however Powershell can.
Said folder changes on each run, so I need to pass it as a variable.

To minimize the imports I want to use os.system.
(Opposed to subprocess.run() and subprocess.popen(), as I had to import system already anyways.)
If its possible, I would also like to keep the code "inline", without having to store and reference a .ps1 file somewhere on my harddrive.

I cobbled together something that does not work via a Google search:

import os

myfolder = "C:/Windows"
os.system("powershell.exe [$qa = New-Object -ComObject shell.application && $qa.NameSpace('%s').Self.InvokeVerb('pintohome')]"  % (myfolder))

I get the following error message:

In Zeile:1 Zeichen:2
  [$qa = New-Object -ComObject shell.application
   ~
Der Typname nach "[" fehlt.
      CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
      FullyQualifiedErrorId : MissingTypename

1.) How to properly tell Python to call powershell?
2.) How to add multiline code so it executes in the same environment?
3.) How to properly pass the folder variable?

As an added bonus, how would I hide the powershell window while executing the code?

CodePudding user response:

import os
myfolder = r"C:\Windows"   # use backslashes!!!

os.system("powershell.exe \"$qa = New-Object -ComObject shell.application; $qa.NameSpace( '%s').Self.InvokeVerb( 'pintohome')\""  % (myfolder))
#                         ↑↑ command as string, see below

Explanation. Command separator in PowerShell is ; (Semicolon). Note that Left and Right Square Bracket ([ and ]) are meta-symbols for optionality in the following description:

powershell.exe -?

PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]
    [-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive]
    [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
    [-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>]
    [-ConfigurationName <string>]
    [-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>]
    [-Command { - | <script-block> [-args <arg-array>]
                  | <string> [<CommandParameters>] } ]

…

-Command
    Executes the specified commands (and any parameters) as though they were
    typed at the Windows PowerShell command prompt, and then exits, unless
    NoExit is specified. The value of Command can be "-", a string. or a
    script block.

    If the value of Command is "-", the command text is read from standard
    input.

    If the value of Command is a script block, the script block must be enclosed
    in braces ({}). You can specify a script block only when running PowerShell.exe
    in Windows PowerShell. The results of the script block are returned to the
    parent shell as deserialized XML objects, not live objects.

    If the value of Command is a string, Command must be the last parameter
    in the command , because any characters typed after the command are
    interpreted as the command arguments.

    To write a string that runs a Windows PowerShell command, use the format:
        "& {<command>}"
    where the quotation marks indicate a string and the invoke operator (&)
    causes the command to be executed.
  • Related