0

I'm trying to run a command in Windows Powershell from Python:

import subprocess, os, sys

subprocess.check_call(['powershell.exe', 'Get-ItemProperty HKLM:/Software/Wow6432Node/Microsoft/Windows/CurrentVersion/Uninstall/* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate, InstallLocation > "C:/Program Files/softwarelist.txt"'])

However, upon sys.stdout.write()-ing the created file with UTF-16 encoding (otherwise it has spaces and compatibility issues all over the place), I am greeted with the following:

DisplayName     : Adobe Flash Player 26 NPAPI

DisplayVersion  : 26.0.0.137

Publisher       : Adobe Systems Incorporated

InstallDate     : 

InstallLocation : 


DisplayName     : Adobe Flash Player 26 PPAPI

DisplayVersion  : 26.0.0.137

Publisher       : Adobe Systems Incorporated

InstallDate     : 

InstallLocation : 


DisplayName     : Agenda At Once 3.3.3

DisplayVersion  : 3.3.3

Publisher       : Dataland Software

InstallDate     : 20161106

InstallLocation : E:\Applications for Work\PIM and Projects\Agenda at Once 
                  PIM\Agenda at Once Components\Agenda At Once\

Everything is as I want it to be, except for one thing: that last InstallLocation string is split into two lines (at the "PIM") in the text file.

Is there a way to avoid this happening?

Itchydon
  • 2,572
  • 6
  • 19
  • 33
  • I would look at using `Export-Csv` in PowerShell to write to a file, and the `csv` module in Python to read the results. – TessellatingHeckler Aug 05 '17 at 00:03
  • 1
    Why are you using Python (scripting) to call another scripting language? Implement everything in one language and make life easier on yourself – Maximilian Burszley Aug 05 '17 at 03:24
  • I agree with [TheIncorrigible1](https://stackoverflow.com/users/8188846/theincorrigible1), anyway, try using ``-Join "`r`n"`` to make it a single string rather than an array: ``subprocess.check_call(['powershell.exe', '(Get-ItemProperty HKLM:/Software/Wow6432Node/Microsoft/Windows/CurrentVersion/Uninstall/* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate, InstallLocation) -Join "`r`n" > "C:/Program Files/softwarelist.txt"'])`` – iRon Aug 05 '17 at 09:12
  • The only reason why I'm using the Powershell command in the first place is to generate a text file that contains (most) of the programs installed on my computer and their information (I'm not aware of any such function within Python). The rest of the program is purely Python-scripted. I have also tried both solutions that you guys provided in the comments; neither has worked. I might also add that this problem is not occurring when I use the same command in the Powershell window. – Daniel Shevchenko Aug 05 '17 at 11:29
  • 1
    Possible duplicate of [Commandline syntax to prevent wrapping in PowerShell output file?](https://stackoverflow.com/q/8973577) Possible duplicate of [Python code to read registry](https://stackoverflow.com/q/5227107). – Ansgar Wiechers Aug 06 '17 at 22:11
  • That first "possible duplicate" was the answer. Thank you :D. – Daniel Shevchenko Aug 08 '17 at 21:45

1 Answers1

1

Possible duplicate of Commandline syntax to prevent wrapping in PowerShell output file?. (Credit to Ansgar Wiechers for answering the question in the comments.)