0

try to make variable input via textbox for this 1.Add-Type -AssemblyName Microsoft.VisualBasic 2.Get-WmiObject -Class Win32_Product | Select-Object -Property Name | Where-Object {$_.Name -like '*putt*'} | Out-File -FilePath C:\programs.txt 3.C:\programs.txt

Try this: 1.Add-Type -AssemblyName Microsoft.VisualBasic 2.$pname = [Microsoft.VisualBasic.Interaction]::InputBox('Program Name:') 3.Get-WmiObject -Class Win32_Product | Select-Object -Property Name | Where-Object {$_.Name -like [$pname')} | Out-File -FilePath C:\programs.txt 4.C:\programs.txt

not work/

Tanks Gocha

Try this: 1.Add-Type -AssemblyName Microsoft.VisualBasic 2.$pname = [Microsoft.VisualBasic.Interaction]::InputBox('Program Name:') 3.Get-WmiObject -Class Win32_Product | Select-Object -Property Name | Where-Object {$_.Name -like [$pname')} | Out-File -FilePath C:\programs.txt 4.C:\programs.txt not work

Tanks Gocha

3
  • 1
    you might want to consider not querying win32_product and query the registry instead. see xkln.net/blog/… Commented Apr 16, 2023 at 14:32
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Apr 16, 2023 at 15:02
  • Please format your post properly - the lack of line breaks and duplicated content makes your question hard to read. Commented Apr 16, 2023 at 17:37

1 Answer 1

1

Santiago Squarzon offers good advice, via this blog post:

  • The use of the Win32_Product WMI class is best avoided, because it is incomplete, slow, and can have side effects.

    • See the linked blog post for a comprehensive - but cumbersome - registry-based solution that covers all users that have installed programs on a given machine.

    • For a convenient, but limited alternative via Get-Package, see the bottom section.

  • Separately, regarding the use of Get-WmiObject:

    • 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) 7+, 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.

The answer to your question as asked:

  • Use Get-CimInstance in lieu of Get-WmiObject, for the reasons explained above.

  • For brevity, use a -Filter argument

    • Note: A -Filter argument translates into an SQL query behind the scenes, which means that the results are filtered at the source, which normally also has performance benefits, but not in the case at hand, as explained here.
Add-Type -AssemblyName Microsoft.VisualBasic
$pname = [Microsoft.VisualBasic.Interaction]::InputBox('Program Name:')

# Pipe the results to Out-File as needed.
Get-CimInstance Win32_Product -Filter "Name LIKE '$pname%'"

A Windows PowerShell alternative,[1] using Get-Package:

Add-Type -AssemblyName Microsoft.VisualBasic
$pname = [Microsoft.VisualBasic.Interaction]::InputBox('Program Name:')

# Pipe the results to Out-File as needed.
Get-Package -ProviderName msi, Programs -Name "$pname*"

This is limited in the sense that, among the programs installed at a user level, only the current user's programs are included.

(Note that querying the Win32_Product class is limited to MSI-installed programs (programs installed by Windows Installer) - I'm unclear on whether that includes user-level programs across all users.)

Also note that the output objects are of a different type than those returned by Get-CimInstance Win32_Product, with different properties.
Pipe output to Get-Member -Type Properties to discover available properties.


[1] Unfortunately, in PowerShell (Core) 7+, as of v7.3.4, the msi and Programs provider are not available, and it's unclear if they'll ever be.

Sign up to request clarification or add additional context in comments.

Comments

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.