Getting Hard Drive Serial Numbers using scripts

Recently I had been working on a way to retrieve hard drive serial numbers without having to go around and look at each drive.  We have to use them for reports and we use equipment from sites that we go to that doesn’t belong to us so a kept list is not an option.  There is software out there that will do it for you and make a nice little report, but our highers up think that is a bad idea and we can just go around a physically look at each drive.  So this has been something we have done for a while.  Well in my efforts to be a lazy admin I found a few things and wrote two scripts.

The first is a vbscript that will write the hard drive serial to a text file along with the host name of the machine.  It calls on WMI to provide the drive serial.  However it only worked with Windows 7 and server 2008.  I didn’t get it to work on XP, which is still our primary OS for user machines.  Here is the script that I planned to copy out to the workstations, execute it remotely, and have it write the information back to the server.

‘ HardDriveSerialNumbers.vbs
‘ Version 0.5 – Oct 2010
Option Explicit
Dim objFSO, objFolder, objShell, objFile, objTextFile, objWMIService, objItem, oshell
Dim strDirectory, strFile, strText, strComputer, colItems, k, serial, strSerial, strSpace, strLine, host
strComputer = “.”
strDirectory = “\\\c$\users\administrator\desktop\testvbs”
strFile = “\HDSerialNumbers.txt”
strSpace = “”
strLine = “=======================================================”
‘ Places Hostname into a variable
Set oShell = CreateObject( “WScript.Shell” )
host=oShell.ExpandEnvironmentStrings(“%ComputerName%” )
‘ Create the File System Object
Set objFSO = CreateObject(“Scripting.FileSystemObject” )
‘ Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Set objFolder = objFSO.CreateFolder(strDirectory)
End If
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
End If
set objFolder = nothing
set objFile = nothing
‘ OpenTextFile Method needs a Const value
‘ ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8
‘ Sets text file to open and modify
Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)
‘ WMI for drive serial numbers will return all drives to include CD drive currently errors on cd drive
On Error Resume Next
Set objWMIService = GetObject(“winmgmts:” & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2” )
Set colItems = objWMIService.ExecQuery(“SELECT * FROM Win32_PhysicalMedia” )
k = 0
serial = “”
For Each objItem in colItems
‘ Writes strText every time you run this VBScript
If err.number = vbEmpty then
Set objShell = CreateObject(“WScript.Shell” ) (“Explorer” & ” ” & strDirectory & “\”  )
Else WScript.echo “VBScript Error: ” & err.number
End If
‘ End of VBScript to create file on DC with Workstation Hard Drive Serial Numbers

Now I am not the best when it comes to vbscript so my structure may be bad but I only care if it works and the making it pretty later.  But once again I could only get this to work in Windows 7 and Server 2008 R2.  For the other operating systems I kept looking and found a little freeware application that will retrieve the hard drive information.  It writes more than what I need but it gets the job done.  The executable is called diskid32.exe, I found it at  It also has a DLL file that is suppose to make the executable work with Windows 2000 and earlier.

Anyhow I work this into a batch file that asks you for the IP of the machine you want to write the information to, then copies the executable and a batch file to each machine, and executes to retrieve the info and write it back to the IP that was inputted at the beginning.  The batch file also uses psexec.exe from  Sysinternals to login to the remote machines to execute the files copied to each machine.  We also use a text file that has the names of all of the machines on the network that we call a nodelist.txt.  So here is what the batch file looks like.

REM     ———————————–
Rem Written by ogwatermelon 8Oct10
REM     ———————————–
@echo off
echo =========================================================
echo                                                                       HDInfo
echo =========================================================
echo Enter the IP Address for the server to write info to.
set /p _host=Enter IP address:
for /f %%D IN (nodelist.txt) DO echo %%D & copy diskid32.exe \\%%D\c$\windows
for /f %%C IN (nodelist.txt) DO echo ==================================================================== >> \\%_host%\c$\hdserials.txt & echo %%C—–%%C—–%%C—–%%C >> \\%_host%\c$\hdserials.txt & echo ===================================================================== >> \\%_host%\c$\hdserials.txt & psexec.exe \\%%C C:\windows\diskid32.exe >> \\%_host%\c$\hdserials.txt
echo finished

The lines of equal signs is to make a recognizable line to look for the hostname and to divide the information in the file that it writes.  The diskid32 executable writes the drive serials, product ids, version numbers, and even the MAC address of the computer.  So the file it writes back it pretty large, but the serials are easy to find.  This will run on XP, Svr 2003, Vista, 7, and Svr 2008.  One of the other admins and I are working on a program to strip out the information that we need from the file and create a report that will be presentable.  It works, gets the correct information, and is free.  So if you need hard drive serial numbers and don’t have other software that can retrieve it for you, feel free to use and modify these scripts to fit your need.  I hope this can help someone.

You may also like...

3 Responses

  1. avatar ogwatermelon says:

    A lot of this depends on the operating system you are trying to get the hard drive serials from. We are still using XP, this makes some of the process different. We ended up using a script to push out a small executable to the workstation then use psexec to run the file. This file would return all of the computer info back to our server where we used vbs script to get only the information we needed and put it into a spreadsheet. The file we used was diskid32.exe, you can find this on the web. If you are using Window 7, you can use a vbs script to get the information directly. However diskid32.exe worked on our newer Windows 7 boxes as well. To execute it we only used the DNS machine name to push out the files. Take a look at diskid32 and see if it will work for you. It returns a lot of information. Here is the link to the website.

  2. avatar A says:

    I need mac address as well. Please help me on this.

  3. avatar Anonymous says:

    I need to get pc name, serial number and mac address as well. Please help me on this.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.