Search code examples
powershellwmi

Getting StdRegProv class in Powershell


The only way I can find to get an instance of the StdRegProv WMI class in Powershell is

$StdRegProv = Get-WmiObject -List -Namespace root\default -ComputerName "my_computer" -Credential $cred | where { $_.Name -eq "StdRegProv" }

I need to go this route because I want to supply a credential. Naively I would have expected the following to work:

$StdRegProv = Get-WmiObject -Class StdRegProv -Namespace root\default -ComputerName "my_computer" -Credential $cred

but it doesn't - why can't you get at StdRegProv in this way?


Solution

  • As far as I understand, Get-WmiObject returns existing instances of a class. If there are no instances, you get an empty collection. (But don't quote me on this because it's just a guess - I haven't looked at Get-WmiObject code.)

    Anyway, you can use:

    [WMIClass]"root\default:StdRegProv"

    to instantiate the class. Or, if it has to be gwmi:

    Get-WmiObject -List -Namespace "root\default" -ComputerName "my_computer" `
        | Where-Object {$_.Name -eq "StdRegProv"}