Get-CimInstance

Get-CimInstance

Gets the CIM instances of a class from a CIM server.

Syntax

Parameter Set: ClassNameComputerSet
Get-CimInstance [-ClassName] <String> [-ComputerName <String[]> ] [-Filter <String> ] [-KeyOnly] [-Namespace <String> ] [-OperationTimeoutSec <UInt32> ] [-Property <String[]> ] [-QueryDialect <String> ] [-Shallow] [ <CommonParameters>]

Parameter Set: CimInstanceComputerSet
Get-CimInstance [-InputObject] <CimInstance> [-ComputerName <String[]> ] [-OperationTimeoutSec <UInt32> ] [-ResourceUri <Uri> ] [ <CommonParameters>]

Parameter Set: CimInstanceSessionSet
Get-CimInstance [-InputObject] <CimInstance> -CimSession <CimSession[]> [-OperationTimeoutSec <UInt32> ] [-ResourceUri <Uri> ] [ <CommonParameters>]

Parameter Set: ClassNameSessionSet
Get-CimInstance [-ClassName] <String> -CimSession <CimSession[]> [-Filter <String> ] [-KeyOnly] [-Namespace <String> ] [-OperationTimeoutSec <UInt32> ] [-Property <String[]> ] [-QueryDialect <String> ] [-Shallow] [ <CommonParameters>]

Parameter Set: QueryComputerSet
Get-CimInstance -Query <String> [-ComputerName <String[]> ] [-Namespace <String> ] [-OperationTimeoutSec <UInt32> ] [-QueryDialect <String> ] [-ResourceUri <Uri> ] [-Shallow] [ <CommonParameters>]

Parameter Set: QuerySessionSet
Get-CimInstance -CimSession <CimSession[]> -Query <String> [-Namespace <String> ] [-OperationTimeoutSec <UInt32> ] [-QueryDialect <String> ] [-ResourceUri <Uri> ] [-Shallow] [ <CommonParameters>]

Parameter Set: ResourceUriComputerSet
Get-CimInstance -ResourceUri <Uri> [-ComputerName <String[]> ] [-Filter <String> ] [-KeyOnly] [-Namespace <String> ] [-OperationTimeoutSec <UInt32> ] [-Property <String[]> ] [-Shallow] [ <CommonParameters>]

Parameter Set: ResourceUriSessionSet
Get-CimInstance -CimSession <CimSession[]> -ResourceUri <Uri> [-Filter <String> ] [-KeyOnly] [-Namespace <String> ] [-OperationTimeoutSec <UInt32> ] [-Property <String[]> ] [-Shallow] [ <CommonParameters>]

Detailed Description

The Get-CimInstance cmdlet gets the CIM instances of a class from a CIM server. You can specify either the class name or a query for this cmdlet.

This cmdlet returns one or more CIM instance objects representing a snapshot of the CIM instances present on the CIM server.

If the InputObject parameter is not specified, the cmdlet works in one of the following ways:

--If neither the ComputerName parameter nor the CimSession parameter is specified, then this cmdlet works on local Windows Management Instrumentation (WMI) using a Component Object Model (COM) session.
--If either the ComputerName parameter or the CimSession parameter is specified, then this cmdlet works against the CIM server specified by either the ComputerName parameter or the CimSession parameter.

If the InputObject parameter is specified, the cmdlet works in one of the following ways:

--If neither the ComputerName parameter nor the CimSession parameter is specified, then this cmdlet uses the CIM session or computer name from the input object.
--If the either the ComputerName parameter or the CimSession parameter is specified, then this cmdlet uses the either the CimSession parameter value or ComputerName parameter value. Note: This is not very common.

Parameters

-CimSession<CimSession[]>

Specifies the CIM session to use for this cmdlet. Enter a variable that contains the CIM session or a command that creates or gets the CIM session, such as the New-CimSession or Get-CimSession cmdlets. For more information, see about_CimSessions.

Aliases

none

Required?

true

Position?

named

Default Value

none

Accept Pipeline Input?

True (ByValue)

Accept Wildcard Characters?

false

-ClassName<String>

Specifies the name of the CIM class for which to retrieve the CIM instances.
NOTE: You can use tab completion to browse the list of classes, because Windows PowerShell gets a list of classes from the local WMI server to provide a list of class names.

Aliases

none

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

True (ByPropertyName)

Accept Wildcard Characters?

false

-ComputerName<String[]>

Specifies computer on which you want to run the CIM operation. You can specify a fully qualified domain name (FQDN), a NetBIOS name, or an IP address.

If you do not specify this parameter, the cmdlet performs the operation on the local computer using Component Object Model (COM).

If you specify this parameter, the cmdlet creates a temporary session to the specified computer using the WsMan protocol.

If multiple operations are being performed on the same computer, using a CIM session gives better performance.

Aliases

CN,ServerName

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

True (ByPropertyName)

Accept Wildcard Characters?

false

-Filter<String>

Specifies a where clause to use as a filter. Specify the clause in either the WQL or the CQL query language.

Note: Do not include the where keyword in the value of the parameter.

Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

True (ByPropertyName)

Accept Wildcard Characters?

false

-InputObject<CimInstance>

Specifies a CIM instance object to use as input.

If you are already working with a CIM instance object, you can use this parameter to pass the CIM instance object in order to get the latest snapshot from the CIM server. When you pass a CIM instance object as an input, Get-CimInstance returns the object from server using a get CIM operation, instead of an enumerate or query operation. Using a get CIM operation is more efficient than retrieving all instances and then filtering them.

If the CIM class does not implement the get operation, then specifying the InputObject parameter returns an error.

Aliases

CimInstance

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

True (ByValue)

Accept Wildcard Characters?

false

-KeyOnly

Indicates that only objects with key properties populated are returned. Specifying the KeyOnly parameter reduces the amount of data transferred over the network.

Use the KeyOnly parameter to return only a small portion of the object, which can be used for other operations, such as the Set-CimInstance or Get-CimAssociatedInstance cmdlets.

Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Namespace<String>

Specifies the namespace of CIM class.

The default namespace is root/cimv2.
NOTE: You can use tab completion to browse the list of namespaces, because Windows PowerShell gets a list of namespaces from the local WMI server to provide the list of namespaces.

Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

True (ByPropertyName)

Accept Wildcard Characters?

false

-OperationTimeoutSec<UInt32>

Specifies the amount of time that the cmdlet waits for a response from the computer.

By default, the value of this parameter is 0, which means that the cmdlet uses the default timeout value for the server.

If the OperationTimeoutSec parameter is set to a value less than the robust connection retry timeout of 3 minutes, network failures that last more than the value of the OperationTimeoutSec parameter are not recoverable, because the operation on the server times out before the client can reconnect.

Aliases

OT

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Property<String[]>

Specifies a set of instance properties to retrieve.

Use this parameter when you need to reduce the size of the object returned, either in memory or over the network.

The object returned always has key properties populated, irrespective of the set of properties listed by the Property parameter. Other properties of the class are present but they are not populated.

Aliases

SelectProperties

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

True (ByPropertyName)

Accept Wildcard Characters?

false

-Query<String>

Specifies a query to run on the CIM server. For WQL-based queries, you can only use a SELECT query that returns instances.

If the value specified contains double quotes (“), single quotes (‘), or a backslash (\), you must escape those characters by prefixing them with the backslash (\) character. If the value specified uses the WQL LIKE operator, then you must escape the following characters by enclosing them in square brackets ([]): percent (%), underscore (_), or opening square bracket ([).

You cannot use a metadata query to retrieve a list of classes or an event query. To retrieve a list of classes, use the Get-CimClass cmdlet. To retrieve an event query, use the Register-CimIndicationEvent cmdlet.

You can specify the query dialect using the QueryDialect parameter.

Aliases

none

Required?

true

Position?

named

Default Value

none

Accept Pipeline Input?

True (ByPropertyName)

Accept Wildcard Characters?

false

-QueryDialect<String>

Specifies the query language used for the Query parameter.

The acceptable values for this parameter are: WQL or CQL.

The default value is WQL.

Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

True (ByPropertyName)

Accept Wildcard Characters?

false

-ResourceUri<Uri>

Specifies the resource uniform resource identifier (URI) of the resource class or instance. The URI is used to identify a specific type of resource, such as disks or processes, on a computer.

A URI consists of a prefix and a path to a resource. For example:

https://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_LogicalDisk
http://intel.com/wbem/wscim/1/amt-schema/1/AMT_GeneralSettings

By default, if you do not specify this parameter, the DMTF standard resource URI http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/ is used and the class name is appended to it.

ResourceURI can only be used with CIM sessions created using the WSMan protocol, or when specifying the ComputerName parameter, which creates a CIM session using WSMan. If you specify this parameter without specifying the ComputerName parameter, or if you specify a CIM session created using DCOM protocol, you will get an error, because the DCOM protocol does not support the ResourceURI parameter.

If both the ResourceUri parameter and the Filter parameter are specified, the Filter parameter is ignored.

Aliases

none

Required?

true

Position?

named

Default Value

none

Accept Pipeline Input?

True (ByPropertyName)

Accept Wildcard Characters?

false

-Shallow

Indicates that the instances of a class are returned without including the instances of any child classes.

By default, the cmdlet returns the instances of a class and its child classes.

Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see    about_CommonParameters (https://go.microsoft.com/fwlink/p/?LinkID=113216).

Inputs

The input type is the type of the objects that you can pipe to the cmdlet.

  • CIM Instance

    This cmdlet accepts an input objects specified with the InputObject parameter.

Outputs

The output type is the type of the objects that the cmdlet emits.

  • CIM Instance

    This cmdlet returns one or more CIM instance objects representing a snapshot of the CIM instances on the CIM server.

Examples

Example 1: Get the CIM instances of a specified class

This command retrieves the CIM instances of a class named Win32_Process.

PS C:\> Get-CimInstance -ClassName Win32_Process

Example 2: Get a list of namespaces from a WMI server

This command retrieves a list of namespaces under the Root namespace on a WMI server.

PS C:\> Get-CimInstance –Namespace root –ClassName __Namespace

Example 3: Get instances of a class filtered by using a query

This command retrieves all the CIM instances that start with the letter p of a class named Win32_Process using the query specified by a Query parameter.

PS C:\> Get-CimInstance -Query "SELECT * from Win32_Process WHERE name LIKE 'p%'"

Example 4: Get instances of a class filtered by using a class name and a filter expression

This command retrieves all the CIM instances that start with the letter p of a class named Win32_Process using the Filter parameter.

PS C:\> Get-CimInstance -ClassName Win32_Process -Filter "Name like 'p%'"

Example 5: Get the CIM instances with only key properties filled in

This set of commands creates a new CIM instance in memory for a class named Win32_Process with the key property @{ "Handle"=0 } and stores it in a variable named $x.

PS C:\> $x = New-CimInstance -ClassName Win32_Process -Namespace root\cimv2 -Property @{ "Handle"=0 } -Key Handle -ClientOnly

The variable is passed as a CIM instance to the Get-CimInstance cmdlet to get a particular instance.

PS C:\> Get-CimInstance -CimInstance $x

Example 6: Retrieve CIM instances and reuse them

This set of commands gets the CIM instances of a class named Win32_Process and stores them in the variables $x and $y. The variable $x is then formatted in a table containing only the Name and the KernelModeTime attributes, the table set to AutoSize.

PS C:\> $x,$y = Get-CimInstance -ClassName Win32_Process
PS C:\> $x| Format-Table -Property Name,KernelModeTime -AutoSize
PS C:\> $x| Get-CimInstance |Format-Table -Property Name,KernelModeTime -AutoSize

Example 7: Get CIM instances from remote computer

This command retrieves the CIM instances of a class named Win32_ComputerSystem from the remote computers named Server01 and Server02.

PS C:\> Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName Server01,Server02

Example 8: Getting only the key properties, instead of all properties

This command retrieves only the key properties, which reduces the size of the object and network traffic.

PS C:\> Get-CimInstance -Class Win32_Process -KeyOnly
PS C:\> $x = Get-CimInstance -Class Win32_Process -KeyOnlyPS C:\>$x | Invoke-CimMethod -MethodName GetOwner

Example 9: Getting only a subset of properties, instead of all properties

This command retrieves only a subset of properties, which reduces the size of the object and network traffic.

PS C:\> Get-CimInstance -Class Win32_Process -Property Name,KernelModeTime
PS C:\> $x = Get-CimInstance -Class Win32_Process -Property Name, KernelModeTimePS C:\>$x | Invoke-CimMethod -MethodName GetOwner

The instance retrieved with the Property parameter can be used to perform other CIM operations, for example Set-CimInstance or Invoke-CimMethod.

PS C:\> $x = Get-CimInstance -Class Win32_Process -Property Name,KernelModeTime
PS C:\> Invoke-CimMethod –InputObject $x -MethodName GetOwner

Example 10: Get the CIM instance using CIM session

This set of commands creates a CIM session on the computers named Server01 and Server02 using the New-CimSession cmdlet and stores the session information in a variable named $s. The contents of the variable are then passed to Get-CimInstance by using the CimSession parameter, to get the CIM instances of the class named Win32_ComputerSystem.

PS C:\> $s = New-CimSession –ComputerName Server01,Server02
PS C:\> Get-CimInstance -ClassName Win32_ComputerSystem -CimSession $s

Get-CimAssociatedInstance" Get-CimAssociatedInstance

Get-CimClass" Get-CimClass

Invoke-CimMethod

New-CimInstance

Register-CimIndicationEvent" Register-CimIndicationEvent

Remove-CimInstance

Set-CimInstance

Format-Table