Use PowerShell to Modify WMI Data Such as Drive Labels

Use PowerShell to Modify WMI Data Such as Drive Labels

  • Comments 1
  • Likes

Summary: The Scripting Wife learns how to use the CIM cmdlets and Windows PowerShell to assign a new drive label by modifying WMI data.

Weekend Scripter: Changing WMI information

Microsoft Scripting Guy, Ed Wilson, is here. Well yesterday, after we got through an extremely long security line at the airport, and we jiggled around in a very cramped airplane for over five hours on our flight to Seattle for the Windows PowerShell summit, we met up with Windows PowerShell MVP Jeff Wouters at the SEATAC airport. We gave him a ride into Redmond and spent some time hanging out. Here is a picture of Jeff and the Scripting Wife I took near the Seattle Space Needle. You see, Jeff has never been to Seattle, and so we thought we would show him around.

clip_image002

The Windows PowerShell summit has already been a success, in my mind, and the first session has not even kicked off. Of course, the biggest thing for the summit for me is not the sessions, but the chance to talk and to interact with members of the community. In this regard, the summit has already exceeded expectations.

I decided to head down to the lobby of the hotel to grab something to eat this morning. It is still very early for people from the West Coast, but for the Scripting Wife and I it is already late due to the three time zone change for us. Of course, for our European friends the change is twice as great. I am sipping a cup of English Breakfast tea, munching on a croissant, and checking my email, when a loud thudding sound accompanied by a sudden shaking of the table woke me from my revelry–it is the Scripting Wife.

I hazard a query, “Why are you are up so early?”

“Hey, it is nearly noon back in Charlotte–I am ready to go,” she chirped.

“Go? Go where?” I ventured.

“Out. We are getting together a group, and we are heading out,” she explained.

“Where is out? Have you no specific destination in mind?” I asked.

“Well, maybe downtown Seattle. There is great shopping there, a cool aquarium, ferry terminals, and wonderful restaurants. We are in a real city my friend, and I for one am not going to let the opportunity pass,” she pontificated.

“I can appreciate that. But before you go, let me show you something you might need to know for the 2013 Scripting Games,” I said.

“OK, but make it quick, because when the group shows up, I am out of here,” she said.

Using the CIM cmdlets to set a WMI property

I opened the Windows PowerShell console with admin rights by right clicking the Windows PowerShell console icon and selecting Run As Administrator from the action menu. Admin rights are required to modify WMI objects.

“OK. Look over here,” I directed. “Find WMI classes related to disks.”

Find disk classes

The Scripting Wife thought for a few minutes, and typed the following.

Get-Cimc<tab><space>*disk*<enter>

The command she typed appears here.

Get-CimClass *disk*

Get dynamic classes

“Now, narrow your output to only dynamic WMI classes,” I instructed.

She used the Up arrow to retrieve the previous command and she added the Dynamic qualifier to her command. Here is what she typed:

<up arrow><space>-q<tab>dynamic<enter>

The command looks like this when complete.

Get-CimClass *disk* -QualifierName dynamic

Get logical disk info

“Cool. Now what we want to do is to find out information about drive C. To do this use the Get-CimInstance cmdlet and retrieve information about the Win32_LogicalDisk WMI class,” I said.

The Scripting Wife thought for a second, and then she began to type. Here is what she did:

Get-CimI<tab><space>Win32_LogicalDisk<enter>

The command looks like the following:

Get-CimInstance win32_logicaldisk

“OK, now look at the output and see if you can tell the property that indicates that it references the drive names,” I said.

She looked at the laptop screen. The output looks like the following:

PS C:\> Get-CimInstance win32_logicaldisk

DeviceID DriveType ProviderName VolumeName Size FreeSpace

-------- --------- ------------ ---------- ---- ---------

C: 3 159486308352 10707682...

D: 3 System Reserved 366997504 113971200

F: 3 ExtraDisk 499738734592 43558884...

Narrow down the drives by letter

“So you did a good job. You found information about all of the logical disks. What I want you to do now, is to return only drive C . To do that you will need to use the Filter property,” I said.

It did not take the Scripting Wife long at all before she used the Up arrow to retrieve her previous command and to add the Filter parameter. Here is what she typed.

<up arrow><space>-f<tab><space>’DeviceID=’c:’”<enter>

Here is the command she created.

Get-CimInstance win32_logicaldisk -Filter "deviceid='c:'"

The command and output from the command are shown here:

PS C:\> Get-CimInstance win32_logicaldisk -Filter "deviceid='c:'"

DeviceID DriveType ProviderName VolumeName Size FreeSpace

-------- --------- ------------ ---------- ---- ---------

C: 3 159486308352 10707628...

Setting a WMI property

“Notice that you have no label, or VolumeName,” I said. “To set that you will use the Set-CimInstance cmdlet and use a hash table for the Property parameter,” I said.

“Huh?” she asked. “You know that I do not speak geek.”

“It makes more sense when you just do it,” I said.

“Well, hurry up. I just got an email on my phone and they will be here in a few minutes,” she said.

“OK. First of all, you need to use the Up arrow and retrieve your previous command. Then you need to pipe the command to the Set-CimInstance cmdlet. Do that, but do not press ENTER because there is more to the command than that,” I said.

The Scripting Wife thought for a few minutes, and then began typing. Here is what she typed.

<Up Arrow><space>Set-Cimi<tab>

She paused.

“OK, now you need to add the Property parameter and an ampersand and an opening and a closing curly bracket. Do not press ENTER,” I said.

This time, she did not hesitate. Here is what she typed:

-p<tab><space>@{}

“Good. Now inside the two curly brackets you add your 'property name equals new value' statement. Here, you are going to set a value for the VolumeName. Give it a value of SSD_Drive,” I said.

Here is what she typed:

volumename='SSD_Drive'

“Awesome. You are nearly there. Add the PassThru parameter so the modified WMI object returns to the Windows PowerShell line, and press ENTER,” I said.

Here is what she typed:

-p<tab><enter>

The complete command is shown here. (This is a one-line command,broken at the pipe so it is more readable.)

Get-CimInstance -ClassName win32_Logicaldisk -Filter "deviceid='c:'" |

Set-CimInstance -Property @{volumename='SSD_Drive'} –PassThru

The command and the associated output are shown in the following image.

clip_image004

 

“Well, isnt that special,” she mocked.

“Actually, I think it is pretty cool,” I said somewhat defensively.

“I do too. I was just giving you a hard time,” She said.

At that instant, the automatic doors to the lobby swung open and in walked a couple of obvious PowerShellers. I could tell by the T-shirts, but the Scripting Wife obviously knew them.

“Well, I am outta here. Have fun working on your presentation,” she said.

“So when will I see you again?” I asked.

“We’re meeting Don Jones and others at Azteca at 5:00,” she said.

“Zulu?”

Without missing a beat, she said “No. Pacific Standard Time.” And she was gone.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Awesome article!  That's really useful stuff, I've actually been pretty unclear on passing a CIM object to Invoke-CimMethod or Set-CimInstance.