views:

125

answers:

1

I am trying to use powershell to get the file version of a file. If I right click the file and look at the version, it shows a value. Here is how I am trying to do it:

$path = "MSDE2000A";
$info = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($path);

Here is the exception information it is throwing:

Exception calling "GetVersionInfo" with "1" argument(s): "MSDE2000A.exe"
At line:1 char:58
+ $f = [system.diagnostics.fileversioninfo]::getversioninfo <<<< ("MSDE2000A.exe")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Every file I've checked has the sames result. However, if my path is c:\windows\notepad.exe (as in the example) it works as expected. What's going on?

+3  A: 

.NET and PowerShell's notion of current directory aren't always the same. Try passing in the absolute path.

[Diagnostics.FileVersionInfo]::GetVersionInfo('C:\Windows\System32\user32.dll')

ProductVersion   FileVersion      FileName
--------------   -----------      --------
6.1.7600.16385   6.1.7600.1638... C:\Windows\System32\user32.dll

Also, you can get this information with Get-ChildItem like so:

Get-ChildItem C:\Windows\System32\user32.dll | fl VersionInfo
Keith Hill