当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何在PowerShell中获取文件版本?

作者:精品下载站 日期:2024-12-14 05:10:54 浏览:14 分类:玩电脑

如何在PowerShell中获取文件版本?


最近,我需要在PowerShell中获取文件版本。这在管理软件更新、验证已安装的应用程序版本或在软件部署过程中特别有用。在这篇博文中,我们将探索使用 PowerShell 获取文件版本的不同方法。

要在 PowerShell 中获取文件版本,您可以使用 Get-Item cmdlet 并访问文件对象的 VersionInfo 属性。例如:

$filePath = "C:\MyFolder\File.exe"
$fileVersion = (Get-Item $filePath).VersionInfo.FileVersion

此命令检索位于 $filePath 的指定文件的版本信息。

在 PowerShell 中获取文件版本

我们将在这里看到 5 种不同的方法来检查 PowerShell 中的文件版本。在这里,我获取了一个示例 .exe 文件,我们将检查该文件的版本。

方法 1:使用 Get-Command

PowerShell 中的 Get-Command cmdlet 通常用于获取计算机上安装的所有命令。但是,它也可用于检索可执行文件的文件版本信息。

这是完整的脚本。

$filePath = "C:\MyFolder\FirefoxInstaller.exe"
$fileVersion = (Get-Command $filePath).FileVersionInfo.FileVersion
Write-Host "The file version is $fileVersion"

在上面的脚本中,我们首先定义要获取其版本信息的可执行文件的路径。然后,我们使用 Get-Command 获取文件的 FileVersionInfo 属性,其中包括版本详细信息。最后,我们使用 Write-Host 输出版本。

您可以在此处看到我使用 VS code 执行脚本后的输出,它向您显示文件版本。

[玩转系统] 如何在PowerShell中获取文件版本?

方法 2:使用 Get-Item

您还可以使用 PowerShell Get-Item cmdlet 获取文件版本信息。此 cmdlet 用于获取指定位置的项目。

$filePath = "C:\MyFolder\FirefoxInstaller.exe"
$fileVersion = (Get-Item $filePath).VersionInfo.FileVersion
Write-Host "The file version is $fileVersion"

在这里,我们使用 Get-Item 检索给定路径中的项目(文件),然后访问其 VersionInfo 属性以获取文件版本。

方法 3:使用 Get-ItemProperty

Get-ItemProperty 是另一个 PowerShell cmdlet,用于检索指定项目的属性。它还可用于提取版本信息。

$filePath = "C:\MyFolder\File.exe"
$fileVersion = (Get-ItemProperty $filePath).VersionInfo.FileVersion
Write-Host "The file version is $fileVersion"

Get-Item类似,我们使用Get-ItemProperty来获取文件的属性,然后访问VersionInfo属性。

方法 4:使用 Get-WmiObject

您还可以将 Get-WmiObjectCIM_DataFile 类结合使用来选择文件的版本。这是一种比较高级的方法,可以用来查询系统上文件的很多信息。

$filePath = "C:\Path\To\Your\File.exe"
$fileVersion = (Get-WmiObject -Class CIM_DataFile -Filter "Name='$filePath'").Version
Write-Host "The file version is $fileVersion"

在此脚本中,我们使用 Get-WmiObject 查询 CIM_DataFile 类以获取指定路径处的文件。然后,我们通过 Name 属性过滤结果以匹配我们的文件路径并检索 Version 属性。

方法 5:使用 System.Diagnostics.FileVersionInfo

您还可以将 .Net 类与 PowerShell 结合使用。您可以利用 System.Diagnostics.FileVersionInfo 类来获取版本信息。

$filePath = "C:\MyFolder\File.exe"
$fileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($filePath)
Write-Host "The file version is $($fileVersionInfo.FileVersion)"

此方法直接利用 .NET Framework 类 FileVersionInfo 来获取文件的版本信息。

高级-可重用功能

您还可以编写自定义函数来检查 PowerShell 中的文件版本。

这是一个完整的脚本示例:

function Get-FileVersion {
    param (
        [string]$filePath
    )

    try {
        if (Test-Path $filePath) {
            $fileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($filePath)
            return $fileVersionInfo.FileVersion
        } else {
            Write-Error "File does not exist: $filePath"
        }
    } catch {
        Write-Error "An error occurred: $_"
    }
}

# Example usage:
$filesToCheck = @(
    "C:\Windows\notepad.exe",
    "C:\Windows\system32\calc.exe"
)

foreach ($file in $filesToCheck) {
    $version = Get-FileVersion -filePath $file
    Write-Host "$file version is $version"
}

此脚本定义了一个函数 Get-FileVersion,该函数将文件路径作为输入,检查文件是否存在,然后使用 .NET FileVersionInfo 类检索文件版本。然后它迭代文件路径数组,检索它们的版本,并将其打印出来。

结论

PowerShell 提供了多种检索文件版本信息的方法。您可以使用各种 PowerShell cmdlet,例如 Get-CommandGet-ItemGet-ItemProperty,或者您想要使用 Get -WmiObject 或 .NET FileVersionInfo 类来获取文件版本。

我希望您现在已经完全了解如何在 PowerShell 中获取文件版本

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯