1

I am new to Powershell and I am having trouble with the following. I need to run a Get-WmiObject command and get programs and versions. Here is the code:

Get-WmiObject -Class Win32_Product | where Name -eq "Microsoft Policy Platform" | select Name, Version >> c:\Temp\PRograms.txt ;
Get-WmiObject -Class Win32_Product | where Name -eq "Mitel Connect" | select Name, Version >> c:\Temp\PRograms.txt

I get this output:

Name                       Version    
----                       -------    
Microsoft Policy Platform  68.1.1010.0

Name          Version       
----          -------       
Mitel Connect 214.100.1252.0

What I am hoping to get is:

Name                            Version    
----                             -------    
Microsoft Policy Platform    68.1.1010.0 

Mitel Connect                214.100.1252.0

I have tried making it 1 line, separate commands, google and looking at like scripts with no luck. I am hoping someone can point me in the right direction so I can build on it.

mclayton
  • 8,025
  • 2
  • 21
  • 26
  • 2
    It’s outputting 2 separate tables because you’re writing two separate sets of results. If you want them in one table you’ll need to combine the results before you write them to the file. For example ```Get-WmiObject -Class Win32_Product | where Name -in @( “Microsoft Policy Platform", “Mitel Connect") | select Name, Version >> c:\Temp\PRograms.txt ;``` – mclayton Jul 19 '22 at 17:03
  • 1
    As an aside: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) v6+, where all future effort will go, doesn't even _have_ them anymore. Note that WMI still _underlies_ the CIM cmdlets, however. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 Jul 19 '22 at 22:24
  • Piggybacking off this so I dont have to make a new post. If I want to check for example 7-zip, but what is listed is 7-Zip 21.07 (x64), how do I only look up 7zip? I have looked for examples and tried trimming the code with no luck. If no one answers, I will create a new question. And thank you mlkemento – BatchKing70 Jul 20 '22 at 15:27

2 Answers2

1

Just preform the WMI query once, and return both programs, and output them both to the text file at once.

Get-WmiObject -Class Win32_Product | where {$_.Name -in ("Microsoft Policy Platform", "Mitel Connect")} | Add-Content c:\Temp\Programs.txt
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
0

Get-package would be much faster in this case, even than using -filter with wmi.

get-package 'Microsoft Policy Platform','Mitel Connect' | 
  select name,version | export-csv programs.csv
js2010
  • 23,033
  • 6
  • 64
  • 66