Windows – Is it possible to get ONLY the Drive Label using WMIC

command linewindows 7wmic

Using WMIC, I want to get only the drive label based on the drive letter:

for /f "skip=1" %a in ('wmic volume where "Driveletter like '%C%'" get label') do echo %a

Now, those of you who understand Windows command line will see why this doesn't work as expected, because I'm "do"ing an echo. So, using the above, the output looks like the following (which isn't what I'm hoping for):

C:\>for /f "skip=1" %a in ('wmic volume where "Driveletter like '%C%'" get label') do echo %a

C:\>echo System

ECHO is on.

The raw output of the WMIC command looks like the following:


The goal is to merely skip the first line (why I was trying to use for /f "skip=1"...) and only get the second line. However, I'm not sure how to merely display this without echoing it.

Best Answer

You have two problems. Techie007 identified the first one - You must prefix your DO command with @ if you want to prevent the command from being echoed to the screen. This is not necessary if ECHO is OFF.

The other problem is a very annoying artifact of how FOR /F interacts with the unicode output of WMIC. Somehow the unicode is improperly converted into ANSI such that each line ends with \r\r\n. FOR /F breaks at each \n, and only strips off a single terminal \r, leaving an extra unwanted \r. (Note that \r represents a carriage return character, and \n a newline character)

WMIC output includes an extra empty line at the end. The extra unwanted \r prevents FOR /F from ignoring the line (it is not seen as empty). When you ECHO the value you get ECHO is on.

There are multiple ways to handle this, but my favorite is to pass the output through an extra FOR /F to remove the unwanted \r.

for /f "skip=1 delims=" %a in ('wmic volume where "Driveletter like '%C%'" get label') do @for /f "delims=" %b in ("%a") do @echo %b
Related Question