如何在 PowerShell 中獲取檔案版本

  1. 使用 Get-Item 在 PowerShell 中獲取檔案版本
  2. 使用 Get-ChildItem 在 PowerShell 中獲取檔案版本
  3. 使用 Get-Command 在 PowerShell 中獲取檔案版本
  4. 使用 System.Diagnostics.FileVersionInfo 在 PowerShell 中獲取檔案版本
  5. 結論
如何在 PowerShell 中獲取檔案版本

檔案的版本號是一個關鍵的信息,特別是在管理和故障排除軟體和系統檔案時。在 PowerShell 中,您可以輕鬆地使用各種 cmdlet 和方法來檢索這些版本信息。

可執行檔案包含版本信息,例如 .exe.dll。請注意,文本檔案沒有任何版本信息。

版本信息包含檔案名稱、檔案版本、公司名稱、產品名稱、產品版本和語言。本教程將教您如何在 PowerShell 中獲取檔案版本。

使用 Get-Item 在 PowerShell 中獲取檔案版本

PowerShell 提供多個 cmdlet 和方法來獲取檔案版本,而 Get-Item 是其中之一。此方法是最簡單且易於使用的方法之一。

語法:

(Get-Item "Path\to\file").VersionInfo.FileVersion
  • Get-Item:這是一個用於檢索指定路徑下的檔案或目錄信息的 PowerShell cmdlet。這僅獲取有關單個檔案或目錄的信息。
  • "Path\to\file":這是提供給 Get-Item 的參數。它指定要檢查的檔案或目錄的路徑。
  • .VersionInfo:這是屬性,允許您訪問和檢查版本信息。
  • .FileVersion:這是 VersionInfo 屬性的子屬性,專門檢索檔案的版本號。

以下示例顯示如何使用 Get-ItemVersionInfo.FileVersion 獲取 C:\Program Files\Google\Chrome\Application\chrome.exe 檔案的版本。

(Get-Item "C:\Program Files\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion

(Get-Item "C:\Program Files\Google\Chrome\Application\chrome.exe") 行使用 Get-Item cmdlet 來檢索位於指定路徑的檔案的信息,也就是給定目錄中的 Chrome 可執行文件(chrome.exe)。然後,在獲得檔案信息後,代碼訪問檔案的 VersionInfo 屬性,該屬性包含有關該檔案的各種詳細信息,包括其版本信息。

最後,.FileVersion 獲取我們感興趣的特定信息,即 Chrome 可執行檔案的版本號。

輸出:

98.0.4758.102

輸出 98.0.4758.102 代表指定路徑下 Chrome 可執行檔案的版本號。在此情況下,它表示安裝在系統上的 Google Chrome 的版本為 98.0.4758.102

使用 Get-ChildItem 在 PowerShell 中獲取檔案版本

Get-ChildItem 獲取一個或多個指定位置的項目和子項目。我們還可以將 Get-ChildItemVersionInfo.FileVersion 屬性結合使用,以在 PowerShell 中獲取檔案版本。

語法:

(Get-ChildItem "Path\to\file").VersionInfo.FileVersion
  • Get-ChildItem:此 cmdlet 用於列出指定位置的檔案和目錄。
  • "Path\to\file":這是提供給 Get-ChildItem 的參數。它指定您想要檢查的檔案或目錄的路徑。
  • .VersionInfo:該屬性允許您訪問和檢查版本信息。
  • .FileVersion:這是 VersionInfo 的子屬性,專門獲取檔案的版本號。

示例:

(Get-ChildItem "C:\Program Files\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion

(Get-ChildItem "C:\Program Files\Google\Chrome\Application\chrome.exe") 行使用 Get-ChildItem cmdlet 來檢索位於指定路徑的檔案的信息,也就是給定目錄中的 Chrome 可執行文件(chrome.exe)。Get-ChildItem 用於列出指定位置的檔案和目錄。

接下來,當檔案信息獲取後,代碼訪問檔案的 VersionInfo 屬性,該屬性包含有關檔案的各種詳細信息,包括其版本信息。最後,.FileVersion 獲取我們感興趣的特定信息,即 Chrome 可執行檔案的版本號。

輸出:

98.0.4758.102

輸出 98.0.4758.102 代表指定路徑下 Chrome 可執行檔案的版本號。在此情況下,它表示安裝在系統上的 Google Chrome 的版本為 98.0.4758.102

使用 Get-Command 在 PowerShell 中獲取檔案版本

Get-Command cmdlet 獲取計算機上安裝的所有命令。它包括所有的 cmdlet、別名、函數、腳本和應用程序。

我們可以使用帶有 FileVersionInfo.FileVersion 屬性的 Get-Command 在 PowerShell 中獲取檔案版本。

語法:

(Get-Command "Path\to\file").FileVersionInfo.FileVersion
  • Get-Command:這是一個用於檢索有關命令、腳本或可執行檔案的信息的 cmdlet。它可以用來查找有關 PowerShell 命令、函數或外部可執行檔案的信息,包括它們的位置屬性。
  • "Path\to\file":這是提供給 Get-Command 的參數。它指定您想要檢查的命令、腳本或可執行檔案的路徑。
  • .FileVersionInfo:這個屬性提供有關命令或腳本的版本信息的詳細信息。
  • .FileVersion:這是 FileVersionInfo 屬性的一個子屬性。它專門獲取命令或腳本的檔案版本。

以下命令獲取 C:\Windows\System32\ActionCenter.dll 檔案的版本號:

(Get-Command C:\Windows\System32\ActionCenter.dll).FileVersionInfo.FileVersion

(Get-Command C:\Windows\System32\ActionCenter.dll) 使用 Get-Command cmdlet 來獲取有關指定命令(在這種情況下是 DLL 檔案)的信息。當我們將檔案路徑作為參數提供給 Get-Command 時,它返回有關該檔案的信息,包括其屬性。

然後,在獲取檔案信息後,代碼訪問檔案物件的 FileVersionInfo 屬性。FileVersionInfo 屬性包含有關檔案的各種詳細信息,包括其版本信息。

最後,.FileVersion 獲取我們感興趣的特定信息,即 ActionCenter.dll 檔案的版本號。

輸出:

10.0.19041.1 (WinBuild.160101.0800)

輸出 10.0.19041.1 (WinBuild.160101.0800) 代表指定路徑下 ActionCenter.dll 檔案的版本號。在此情況下,它表示系統上 ActionCenter.dll 檔案的版本為 10.0.19041.1 (WinBuild.160101.0800)

使用 System.Diagnostics.FileVersionInfo 在 PowerShell 中獲取檔案版本

此方法是通過檔案路徑(您想要檢索版本信息的實際檔案路徑)作為參數來調用的。此方法調用檢索指定檔案的版本信息。

來自 .NET 框架的 System.Diagnostics.FileVersionInfo 類提供檔案的版本信息。我們可以使用 GetVersionInfo() 方法和 FileVersion 屬性來獲取檔案版本號。

語法:

[System.Diagnostics.FileVersionInfo]::GetVersionInfo("Path\to\file").FileVersion
  • [System.Diagnostics.FileVersionInfo]:這部分指定了用於訪問檔案版本信息的 .NET 框架類。此 .NET 類提供訪問有關檔案的版本信息,通常是可執行檔案,但也可用於其他類型的檔案。
  • GetVersionInfo:這是檢索指定於參數中的檔案版本信息的方法。
  • "Path\to\file":這是提供給 GetVersionInfo 方法的參數。這指定了您想要檢查的檔案的確切路徑。
  • .FileVersion:這個屬性專門獲取該檔案的版本號。檔案版本通常是表示檔案或軟體版本的數值。

示例:

[System.Diagnostics.FileVersionInfo]::GetVersionInfo("C:\Windows\System32\ActionCenter.dll").FileVersion

[System.Diagnostics.FileVersionInfo]::GetVersionInfo("C:\Windows\System32\ActionCenter.dll") 行使用 [System.Diagnostics.FileVersionInfo] 類,它是 .NET 框架的一部分,以獲取有關指定檔案的版本信息。:: 語法用於訪問 FileVersionInfo 類的靜態方法 GetVersionInfo,而路徑 "C:\Windows\System32\ActionCenter.dll" 作為參數提供給此方法。

然後,在獲取版本信息後,代碼訪問 FileVersionInfo 物件的 FileVersion 屬性。最後,FileVersion 屬性包含一個表示該檔案版本的字符串。

輸出:

10.0.19041.1 (WinBuild.160101.0800)

輸出 10.0.19041.1 (WinBuild.160101.0800) 代表 ActionCenter.dll 檔案的版本號。在此情況下,它表示系統上 ActionCenter.dll 檔案的版本為 10.0.19041.1 (WinBuild.160101.0800)

結論

掌握在 PowerShell 中檢索檔案版本信息是一項有效檔案管理和故障排除的基本技能。在整篇文章中,我們學習了四種方法,使我們能夠輕鬆提取數據。

無論我們選擇使用 Get-ItemGet-ChildItemGet-Command,還是 System.Diagnostics.FileVersionInfo 方法,現在我們都擁有了一個多功能的工具包來獲取檔案版本詳情。

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
作者: Rohan Timalsina
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

相關文章 - PowerShell File