nik29kit
nik29kit

Reputation: 13

-List property in cmdlet Get-WmiObject

I know that -List gets the names of the WMI classes in the WMI repository namespace, but I don't really understand what it means in the following context:

(Get-WmiObject -list Win32_ShadowCopy).Create("C:\","ClientAcessible")

Upvotes: 1

Views: 4224

Answers (1)

mklement0
mklement0

Reputation: 437109

Note: The CIM cmdlets have superseded the WMI cmdlets, but the answer applies somewhat analogously, except that Get-CimInstance doesn't support -List, but there's a dedicated Get-CimClass cmdlet and the simplest way to invoke class methods is to always use the dedicated Invoke-CimMethod cmdlet.


(Get-WmiObject -list Win32_ShadowCopy).Create("C:\","ClientAcessible")

uses the -List parameter to get access to the Win32_ShadowCopy class, so as to be able to then instantiate it via its .Create() method, which requires arguments.

By contrast, instances of many WMI classes do not require arguments, so a simple Get-WmiObject <class-name> call is usually sufficient; e.g.:

Get-WmiObject Win32_ComputerSystem # no arguments needed; returns instance directly

Ansgar Wiechers points out that a simpler (and faster) way to obtain a WMI class is by casting its name to type accelerator [wmiclass], so the equivalent of your -List-based command is:

([wmiclass] Win32_ShadowCopy).Create("C:\","ClientAcessible")

That said, the more PowerShell-like way to invoke a method on a WMI class is to use
Invoke-WmiMethod
. Again, the equivalent of the -List-based command is:

Invoke-WmiMethod Win32_ShadowCopy -Name Create -ArgumentList "C:\", "ClientAcessible"

As for the general purpose of the -List parameter:

Let's ask PowerShell's own help system, also available online:

PS> Get-Help Get-WmiObject -Parameter List

-List [<SwitchParameter>]
    Gets the names of the WMI classes in the WMI repository namespace that is specified 
    by the Namespace parameter.

    If you specify the List parameter, but not the Namespace parameter, 
    Get-WmiObject uses the Root\Cimv2 namespace by default. 
    This cmdlet does not use the Default Namespace registry entry in the
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WBEM\Scripting registry key 
    to determine the default namespace.

In other words: -List's purpose is to list (enumerate) WMI classes, optionally filtered by class-name pattern:

  • If you don't explicitly use -NameSpace, ROOT\Cimv2 is assumed.

  • The first positional parameter - Win32_ShadowCopy in your case - binds to the -ClassName (-Class) parameter, which in this case acts as a filter.

    • Since Win32_ShadowCopy is a literal class name, only that very class is matched, but you can use wildcards too (see below).

E.g., to find all WMI classes (in namespace ROOT\Cimv2) whose name contains the word shadow, use:

PS> Get-WmiObject -List -Class *shadow*


   NameSpace: ROOT\cimv2

Name                                Methods              Properties
----                                -------              ----------
Win32_ShadowProvider                {}                   {Caption, CLSID, Description, ID...}
Win32_ShadowCopy                    {Create, Revert}     {Caption, ClientAccessible, Count, Description...}
Win32_ShadowContext                 {}                   {Caption, ClientAccessible, Description, Differential...}
Win32_ShadowStorage                 {Create}             {AllocatedSpace, DiffVolume, MaxSpace, UsedSpace...}
Win32_ShadowVolumeSupport           {}                   {Antecedent, Dependent}
Win32_ShadowFor                     {}                   {Antecedent, Dependent}
Win32_ShadowOn                      {}                   {Antecedent, Dependent}
Win32_ShadowBy                      {}                   {Antecedent, Dependent}
Win32_ShadowDiffVolumeSupport       {}                   {Antecedent, Dependent}

Upvotes: 2

Related Questions