Done Disk name

oph

Jun 28, 2008
30
0
#1
The command

echo %@serial[C:]

shows the "soft" serial of a disk or partition.

But this number can be changed, for example when one does a disk clone
of a disk: you end with two (or more) disk(s) with the same serial.

But the disk serial hardwired by the disk maker is unique.

This number can be obtained, say for disk 0:

wmic diskdrive 0 get serialnumber

For example:

wmic diskdrive 0 get serialnumber S e r i a l N u m b e r

1 7 1 4 2 1 8 0 2 0 3 1

wmic diskdrive 1 get serialnumber S e r i a l N u m b e r

W D - W M C 6 N 0 E 4 6 3 2 T

wmic diskdrive 2 get serialnumber S e r i a l N u m b e r

W D - W C C 4 M 1 L A 9 F L 3

The output of wmic has NULL characters between serial number
characters.

If one must take a option based in disk (hard) serial, it is difficult
to do.

Is it possible to obtain a string without separator, and without the
header "Serial number"?

Thank you.

OPH.
 
May 29, 2008
533
3
Groton, CT
#2
I had some luck doing this:
Code:
alias drivenum=`(wmic diskdrive %@eval[0%1] get serialnumber) > x.tmp & tail /n1 x.tmp`
drivenum 0
ADEADBEEF  (<--fake hexadecimal result)
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,609
46
Albuquerque, NM
prospero.unm.edu
#4
wmic diskdrive 0 get serialnumber S e r i a l N u m b e r

1 7 1 4 2 1 8 0 2 0 3 1

wmic diskdrive 1 get serialnumber S e r i a l N u m b e r

W D - W M C 6 N 0 E 4 6 3 2 T

wmic diskdrive 2 get serialnumber S e r i a l N u m b e r

W D - W C C 4 M 1 L A 9 F L 3

The output of wmic has NULL characters between serial number
characters.
Incidentally, this behavior (NULs between spaces) is what you see when you try to read a UTF-16 text file as if it were 8-bit text.
 
#5
Hmmm! Both WMIQUERY and @WMI work when elevated.
Code:
v:\> wmic diskdrive 0 get serialnumber
SerialNumber
2020202020202020202020205239345752375641


v:\> wmiquery root\cimv2 "select serialnumber from win32_diskdrive where index = 0"
2020202020202020202020205239345752375641

v:\> echo %@wmi[root\cimv2,"select serialnumber from win32_diskdrive where index = 0"]
2020202020202020202020205239345752375641
When not elevated, @WMI quietly returns an empty string while WMIQUERY gives a peculiar error message.
Code:
v:\> echo %@wmi[root\cimv2,"select serialnumber from win32_diskdrive where index = 0"]
ECHO is OFF

v:\> wmiquery root\cimv2 "select serialnumber from win32_diskdrive where index = 0"
Error (SYS): Incorrect function.
I wonder how WMIQ.EXE manages to work unelevated.

I also wonder why my output is so ugly compared to oph's.
 
#6
OK, now @WMI and WMIQUERY are working unelevated. And, come to think of it, my outout isn't all that ugly. Al least it doesn't look like Unicode, but it does have all thgose leading "20"s. If I treat the output of WMIC, WMIQUERY, and @WMI as hex and disregard the initial "20"s, it looks nicer.
Code:
v:\> echo %@wmi[root\cimv2,"select serialnumber from win32_diskdrive where index = 0"]
2020202020202020202020205239345752375641

v:\> echo %@char[0x52 0x39 0x34 0x57 0x52 0x37 0x56 0x41]
R94WR7VA
 
#7
I googled some, and found this to be a small nightmare. The serial number is a string, but the manufacturer is free to "encode" the string as he sees fit. And apparently, since there are no rules, Windows can only guess what to do with the string it finds.

I can get the same hex-coded (?) string as before like this (unelevated).
Code:
v:\> echo %@ltrim[" ",%@wmi[root\cimv2,"SELECT SerialNumber FROM Win32_PhysicalMedia where tag = '\\\\.\\PHYSICALDRIVE0'"]]
2020202020202020202020205239345752375641
And I can manually "decode" it like this, taking the bytes in order.
Code:
v:\> do i=0 to 38 by 2 ( set hex=%@instr[%i,2,2020202020202020202020205239345752375641] & if %hex NE 20 echos %@char[0x%hex] )
R94WR7VA
If I do the very same query elevated, watch what happens!
Code:
v:\> echo %@ltrim[" ",%@wmi[root\cimv2,"SELECT SerialNumber FROM Win32_PhysicalMedia where tag = '\\\\.\\PHYSICALDRIVE0'"]]
9RW47RAV
It's decoded for me ... well sort of. Each pair of characters has had it's order switched.

So what is the serial number of that disk?
 
Jan 19, 2011
581
10
Norman, OK
#9
#10
9RW47RAV

Non-elevated, my drive returns
Code:
echo %@ltrim[" ",%@wmi[root\cimv2,"SELECT SerialNumber FROM Win32_PhysicalMedia where tag = '\\\\.\
\PHYSICALDRIVE0'"]]
W -DXW151CF07810
But Western Digital drive serial numbers start with "WD-" so there's your answer.
Here, it looks good for a WD (elevated or not).
Code:
v:\> echo %@wmi[root\cimv2,"SELECT SerialNumber FROM Win32_PhysicalMedia where tag = '\\\\.\\PHYSICALDRIVE2'"]
WD-WXE508CJ3523

v:\> echo %@wmi[root\cimv2,"select serialnumber from win32_diskdrive where index = 2"]
WD-WXE508CJ3523

v:\> wmic diskdrive 2 get serialnumber
SerialNumber
WD-WXE508CJ3523
What was the answer?
 

oph

Jun 28, 2008
30
0
#11
HWINFO (it is a free program) serial number agrees with wmic:

wmic diskdrive get serialnumber

S e r i a l N u m b e r

W D - W M C 6 N 0 E 4 6 3 2 T

W D - W C C 4 M 1 L A 9 F L 3

1 7 1 4 2 1 8 0 2 0 3 1

The last disk is a SSD and I have the number accesible: it agrees 171421802031
 

Attachments

Aug 23, 2010
249
2
#12
The big question is - for what purpose you are trying to obtain these strings.
Hardware serial is rather useless for OS behavior, for instance.