Hey, Scripting Guy! How can I extract version information for a DLL or other type of file?
Hey, DM. There at least two ways we know of to retrieve the version information for a file: one designed for working on the local computer and one that can work either locally or remotely. Being the kind of guys that we are, we’ll show you both solutions.
Ok, being the kind of guys that we really are, we’d charge you for both solutions. But Microsoft won’t let us do that, so you get both scripts for free.
Of course, if you want to send us a check anyway….
Let’s start with the version designed for the local computer. This script uses the FileSystemObject and requires a grand total of 2 lines of code:
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
As you can see, there isn’t much work involved here (one reason why it’s a Scripting Guys favorite). We simply create an instance of the FileSystemObject and then echo the value returned by the GetFileVersion method. And there isn’t much work involved in calling GetFileVersion, either; in fact, all we have to do is specify the file in question – in this case, C:\Windows\System32\Scrrun.dll – and the FileSystemObject will do the rest.
So if that’s easy why don’t we just recommend this script and be done with it? Well, for two reasons. First, the FileSystemObject is designed for use on the local computer; it’s not really designed to work against remote machines. (You can often use it against remote machines as long as you specify an administrative share: \\atl-ws-01\C$\Windows\System32\Scrrun.dll. However, even this doesn’t always work.)
Second, GetFileVersion does only one thing: it returns the version number of a file. (And by file we generally mean a .dll or .exe file; those are typically the only files that actually have version numbers.) What if you wanted some additional information, like the file size or the last modification date? GetFileVersion won’t help you there; you’d have to combine our two-line script with some additional code in order to get a more complete set of file properties.
Because of that you might want to use WMI to determine the version of a file. WMI works just as well remotely as it does locally; in addition, the CIM_Datafile class used to determine the file version can also return all sorts of other information about a file. The only drawbacks? A WMI script is a tad bit more complicated (but just a tad) and runs a tiny bit slower than a FileSystemObject script. Other than that, however, the two scripts are identical: they both return the version of a specified file.
Here’s a WMI script that reports back the version of the file C:\Windows\System32\Scrrun.dll:
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colFiles = objWMIService.ExecQuery _
(“Select * from CIM_Datafile Where Name = ‘c:\\windows\\system32\\scrrun.dll'”)
For Each objFile in colFiles
Like we said, not too bad. The only thing to be aware of is the way we format path names in our query:
“Select * from CIM_Datafile Where Name = ‘c:\\windows\\system32\\scrrun.dll'”
Note that we had to use \\ throughout the path. That’s because the \ is a reserved character in WMI, and any time we want to use a \ in a Where clause we have to preface it with another \. It’s not a big deal; just something to keep in mind.
Other than that the script is pretty straightforward. This sample script returns information about the Scrrun.dll file found on the local computer. If you’d like to get that same information off a remote machine then change the value of the variable strComputer. For example, this line of code assigns the value atl-ws-01 to the variable strComputer:
strComputer = “atl-ws-01”
In turn, that will cause the script to run against the computer atl-ws-01.