Generally, it is the best to move away from the old and deprecated Get-WmiObject command and instead use the modern and faster CIM cmdlets like Get-CimInstance. In most scenarios, they almost work the same.
In a few areas, though, the new CIM cmdlets lack information. One of the most important areas is the property “__Path” that was available with any object returned by Get-WmiObject. With Get-CimInstance, it is missing.
This path is the unique path to a WMI instance. Have a look – this command lists all instances of Win32_Share and returns the WMI paths to these instances:
PS> Get-WmiObject -Class Win32_Share | Select-Object -ExpandProperty __Path
\\DESKTOP-8DVNI43\root\cimv2:Win32_Share.Name="HP Universal Printing PCL 6"
\\DESKTOP-8DVNI43\root\cimv2:Win32_Share.Name="OKI PCL6 Class Driver 2"
Once you know the path to an instance, you can always easily access it via the [wmi] type:
PS> [wmi]'\\DESKTOP-8DVNI43\root\cimv2:Win32_Share.Name="HP Universal Printing PCL 6"'
Name Path Description
---- ---- -----------
HP Universal Printing PCL 6 S/W Laser HP,LocalsplOnly S/W Laser HP
This is extremely useful, and you can use existing WMI paths or construct your own. For example, to access the C$ share on a different server, by looking at the path you immediately identify the part that needs change:
The __Path property made “WMI explorers” like this one possible, too:
# get all WMI instances
Get-WmiObject -Class CIM_LogicalDevice |
# display propertes
Select-Object -Property __Class, Name, Description, __Path |
# let user select some
Out-GridView -Title 'Select one or more (hold CTRL)' -PassThru |
# retrieve the full selected instance by path
[wmi]$_.__Path | Select-Object * | Out-Default
PowerShell Conference Europe (psconf.eu) opens June 2, 2020, in Hannover, Germany, and you can be part of it! 4 days, 3 tracks, 80 PowerShell sessions, and 40 renown speakers from around the world (including PowerShell inventor Jeffrey Snover, the PowerShell team with Steve Lee, the Amazon AWS team, and many more) are waiting for questions and discussions, providing authoritative firsthand information, tips and guidance for professional PowerShell scripters.
Find out more at http://powershell.one/psconfeu/psconf.eu-2020/about, download the mobile app with sessions and speakers at http://psconfeu.sessionize.com/, and secure your seat at https://psconf.eu/register.html.
ReTweet this Tip!
Powered by IDERA