Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Library for Powershell 6 contains the get-wmiobject command?

I am getting the following error upon attempting to use the get-WmiObject command in PowerShell (version 6):

PS C:\Users\zsofi> Get-WmiObject Win32_product | select name, packagecache

Get-WmiObject : The term 'Get-WmiObject' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Get-WmiObject Win32_product | select name, packagecache
+ ~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException`
like image 552
Zsofia Avatar asked Feb 02 '19 16:02

Zsofia


People also ask

What is get-WmiObject in PowerShell?

The Get-WmiObject cmdlet gets instances of WMI classes or information about the available WMI classes. To specify a remote computer, use the ComputerName parameter. If the List parameter is specified, the cmdlet gets information about the WMI classes that are available in a specified namespace.

How do I run a WMI query in PowerShell?

So how do you use the WQL query to retrieve information from a system? In Windows PowerShell 2.0, there are two main ways to do this. The first is to use the Get-WmiObject cmdlet, and the second is to use the [wmisearcher] type accelerator. The [wmisearcher] type accelerator creates a ManagementObjectSearcher class.

What is the primary difference between Get-WmiObject and get CimInstance in PowerShell?

Get-WmiObject is formatting its output as a list, while Get-CimInstance is displaying its output in table form. The actual information that is being displayed is mostly the same. This isn't to say that there are no differences between the two cmdlets.


2 Answers

Gert Jan Kraaijeveld's helpful answer offers a solution for cmdlets that truly are available only in Windows PowerShell (not also in PowerShell [Core] 6+).

In this particular case, however, as Lee_Daily notes in a comment, you can use the
Get-CimInstance cmdlet
, which is available in PowerShell [Core] 6+ too:

Get-CimInstance CIM_Product | Select-Object Name, PackageCache

Note the CIM_Product class name; CIM classes typically have the same properties as their WMI Win32_* counterparts.

Why you should generally use the CIM cmdlets instead of the WMI cmdlets:

In PowerShell Core, where all future development effort will go, the CIM cmdlets are your only option, but it is advisable to use the CIM (*-Cim*) cmdlets even in Windows PowerShell, because the WMI (*-Wmi*) cmdlets were deprecated in PowerShell version 3 (released in September 2012), when the CIM cmdlets were introduced; from the Get-CimInstance docs:

Starting in Windows PowerShell 3.0, this cmdlet has been superseded by Get-CimInstance.

As for why the CIM cmdlets are the better choice (quoted from this TechNet blog post):

The big drawback to the WMI cmdlets is that they use DCOM to access remote machines. DCOM isn’t firewall friendly, can be blocked by networking equipment, and gives some arcane errors when things go wrong.

The same blog post also describes how the CIM cmdlets:

  • use the same standards-based remoting mechanism as PowerShell itself (WS-Management, via its Windows implementation, WinRM)

    • That is, computers that are set up for PowerShell remoting (see about_Remote_Requirements) implicitly support targeting via the CIM cmdlets.

    • However, you can still use the DCOM protocol (like the WMI cmdlets did) on an opt-in basis, using the New-CimSessionOption cmdlet.

  • have support for sessions

  • function slightly differently than their obsolete WMI counterparts in that the objects returned do not have methods directly; methods must be called via Invoke-CimMethod.

like image 166
mklement0 Avatar answered Oct 02 '22 03:10

mklement0


As far as I know the only way is the Compatibility module. This is a very neat module by Microsoft that actually makes Windows PS cmdlets available in PS Core by means of implicit remoting to a Windows Powershell 5.1 session on the same machine. https://github.com/PowerShell/WindowsCompatibility

like image 29
Gert Jan Kraaijeveld Avatar answered Oct 02 '22 02:10

Gert Jan Kraaijeveld