3

I am attempting convert a PowerShell command that generates a list of applications that are currently installed on a device in Python to use that list in an application uninstaller.

The command I am working with is:

Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall* |
    Select-Object DisplayName, DisplayVersion, UninstallString

When attempting to convert the command I get the error:

'Select-Object' is not recognized as an internal or external command, operable program or batch file.

My example Python code is as follows:

shell = "powershell.exe "
shell_command = "Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall*"
followup_command = "Select-Object DisplayName, DisplayVersion, UninstallString"

result = subprocess.run(
    shell + shell_command + " | " + followup_command,
    encoding="UTF-8",
    stdout=subprocess.PIPE,
)

result = result.stdout

print(result)

Where am I going wrong?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Monpolyme1
  • 33
  • 4
  • It's your OS - Windows - which doesn't recognize that command. I would say your script works, e.g. your python scripts tells your os what to do, it's just that what your telling your os doesn't make sense to it – logicOnAbstractions Jul 26 '19 at 23:34
  • 1
    Try prepending `Import-Module Microsoft.PowerShell.Utility;` – Mathias R. Jessen Jul 27 '19 at 13:31
  • It's very unlikely that your PowerShell would recognize `Get-ItemProperty` but not `Select-Object`. Also, your registry path is missing a backslash between "Uninstall" and "\*". With that said, after fixing the path your sample code worked just fine for me. Please show your Windows version (`(gwmi Win32_OperatingSystem).Caption` or `[Environment]::OSVersion`), PowerShell version (`$PSVersionTable`) and Python version (`python -V`). – Ansgar Wiechers Jul 27 '19 at 13:34
  • Ansgar Nailed it. This is due to a typeo in converting my command. I was missing the \\ at the end of shell_command variable. – Monpolyme1 Jul 29 '19 at 18:27

0 Answers0