In many situations, you will want to limit the data you return to a specific instance of that class in the dataset. If you go back to your query and add a Where clause to Select statement, you’ll be able to greatly reduce the amount of information returned by the query. Notice that in the value associated with the wmiQuery, you added a dependency that indicated you wanted only information with share name C$. This value is not case sensitive, but it must be surrounded with single quotation marks, as you can see in the wmiQuery string in the following script. These single quotation marks are important because they tell WMI that the value is a string value and not some other programmatic item. Because the addition of the Where statement was the only thing you really added to the ListShares.ps1 script, we do not provide a long discussion of the ListSpecificShares.ps1 script.
ListSpecificShares.ps1
$strComputer = "." $wmiNS = "root\cimv2" $wmiQuery = "Select * from win32_share where name='c$'" $objWMIServices = Get-WmiObject -computer $strComputer -namespace $wmiNS -query $wmiQuery $objWMIServices | Format-List *
To limit specific data
Make a connection to WMI.
Use the Select statement in the WMIQuery argument to choose the specific property you are interested in, for example, Select name.
Use the From statement in the WMIQuery argument to indicate the class from which you want to retrieve data, for example, From Win32_Share.
Add a Where clause in the WMIQuery argument to further limit the dataset that is returned. Make sure the properties specified in the Where clause are first mentioned in the Select statement, for example, Where name.
Add an evaluation operator. You can use the equals sign (=), or the less than (<) or greater than (>) symbols, for example, Where name = 'C$'
Open Notepad or some other Windows PowerShell script editor.
Declare a variable called $strComputer and assign the WMI shortcut dot (.) to it. The shortcut dot means connect to the WMI service on the local computer. This command is shown here:
$strComputer = "."
Declare another variable and call it $wmiClass. Assign the string "WIN32_Share" to the variable. This code is shown here:
$wmiClass = "win32_Share"
Declare a variable and call it $wmiFilter. This variable will be used to hold the string that will contain the WMI filter to be used with the Get-WmiObject command. The variable and the associated string value are shown here:
$wmiFilter = "name='c$'"
Declare a variable called objWMIServices and assign the object that is returned from the Get-WmiObject cmdlet to the variable. Specify the computer argument and supply the value contained in the $strComputer variable to it. At the end of the line, use the grave accent character (`) to indicate line continuation. This line of code is shown here:
$objWMIServices = Get-WmiObject -computer $strComputer `
Use the class argument to supply the class name for the WMI query to the Get-WmiObject cmdlet. The class name to query is contained in the $wmiClass variable. On the same line, use the filter argument to supply the filter string contained in the $wmiFilter variable to the Get-WmiObject cmdlet. This line of code is shown here:
-class $wmiClass -filter $wmiFilter
On the next line, use the object contained in the $objWMIServices variable and pipeline it to the Format-List cmdlet. Use the asterisk to tell the Format-List cmdlet you wish to retrieve all properties. This line of code is shown here:
$objWMIServices | Format-List *
The completed script is shown here:
$strComputer = "." $wmiClass = "win32_Share" $wmiFilter = "name='c$'" $objWMIServices = Get-WmiObject -computer $strComputer ` -class $wmiClass -filter $wmiFilter $objWMIServices | Format-List *
A sample output is shown here:
Status : OK Type : 2147483648 Name : C$ __GENUS : 2 __CLASS : Win32_Share __SUPERCLASS : CIM_LogicalElement __DYNASTY : CIM_ManagedSystemElement
If your results are not similar, compare your script with the ShareNoQuery.ps1 script.
This completes the eliminating the WMIQuery argument procedure.