3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[備忘録] PowerShellで確認できるWindowsのOSバージョンとビルド番号

3
Posted at

はじめに

powershell-windows-version-diagram.png

本記事では、PowerShellでWindowsのOSバージョンやビルド番号を確認する方法を備忘録としてまとめています。毎回「winver」で確認する手間を省きたい方や、自動取得で効率化したい方におすすめです。既出かな感じだと思いますが気なったかたは参照ください。

確認方法まとめ

1. [System.Environment]::OSVersion

もっともシンプルに取得できる方法。

[System.Environment]::OSVersion

出力例:

Platform ServicePack Version      VersionString
-------- ----------- -------      --------------
Win32NT             10.0.19045.0 Microsoft Windows NT 10.0.19045.0
  • Version プロパティ → 10.0.19045.0 形式
  • ただし「正確なエディション情報」までは出ないので用途は限定的
  • 用途: 簡易確認、バージョン番号のみ必要な場合

2. Get-ComputerInfo

PowerShell 5.1以降で利用可能(Windows 10/11標準搭載)。詳細情報がまとめて取得できます。

Get-ComputerInfo | Select-Object OsName, OsArchitecture, WindowsVersion, WindowsBuildLabEx

出力例:

OsName             : Microsoft Windows 11 Pro
OsArchitecture     : 64-bit
WindowsVersion     : 22H2
WindowsBuildLabEx  : 22621.2134.amd64fre.ni_release_svc_prod3.230804-1419
  • WindowsVersion → 「22H2」などマーケティング的なバージョン表記
  • WindowsBuildLabEx → 内部的なビルド番号とブランチ
  • 用途: 詳細情報の一括取得、レポート作成

3. Get-ItemProperty レジストリ参照

レジストリの HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion から直接取得。

Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" |
    Select-Object ProductName, ReleaseId, CurrentBuild, CurrentBuildNumber, DisplayVersion

出力例:

ProductName       : Windows 11 Pro
ReleaseId         : 2009
CurrentBuild      : 22621
CurrentBuildNumber: 22621
DisplayVersion    : 22H2
  • ProductName → Windowsのエディション
  • DisplayVersion → 21H2 / 22H2 など実際の表示バージョン
  • 長期的に安定して参照できるため、スクリプトに組み込みやすい
  • 用途: スクリプト組み込み、最も確実な方法

4. WMI (CIM) 経由

WMIクラスから情報を取得する方法。古い環境との互換性が高い。

Get-CimInstance Win32_OperatingSystem | 
    Select-Object Caption, Version, BuildNumber

出力例:

Caption        : Microsoft Windows 10 Pro
Version        : 10.0.19045
BuildNumber    : 19045
  • Caption → OS名(Pro/Educationなど)
  • Version / BuildNumber → バージョンとビルド番号
  • 用途: 古い環境を含む互換性重視の場合

方法別の使い分けガイド

方法 利点 適用場面
[System.Environment]::OSVersion 最速・最軽量 簡易確認用
Get-ComputerInfo 情報量が豊富 詳細レポート作成
レジストリ参照 最も確実・安定 スクリプト組み込み
WMI/CIM 高い互換性 古い環境対応

実装例

OSバージョンが22H2未満の場合に警告する簡易スクリプト

# DisplayVersionを取得
$ver = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").DisplayVersion

# 文字列として比較(またはバージョン番号を数値化して比較)
if ($ver -lt "22H2") {
    Write-Warning "このPCはサポート対象外のバージョンです: $ver"
} else {
    Write-Host "現在のバージョン: $ver (サポート対象)" -ForegroundColor Green
}

より詳細な情報を含むログ出力例

# OS情報を収集
$osInfo = Get-ComputerInfo | Select-Object `
    OsName, 
    OsArchitecture, 
    WindowsVersion, 
    WindowsBuildLabEx,
    OsInstallDate

# ログファイルに出力
$logPath = "C:\Logs\OSInfo_$(Get-Date -Format 'yyyyMMdd').txt"
$osInfo | Out-File -FilePath $logPath -Append

Write-Host "OS情報を $logPath に記録しました"

セキュリティとトラブルシューティング

これらのコマンドは基本的に一般ユーザー権限で実行できますが、企業環境ではグループポリシーでレジストリアクセスが制限されることがあります。その場合は Get-ComputerInfo や WMIを利用するとよいでしょう。なお、PowerShellのバージョンが古いと Get-ComputerInfo が使えず、またレジストリやWMIにアクセスできない場合は権限やサービスの問題が原因となるため、状況に応じて代替手段やサービス再起動を検討してください。

まとめ

image.png

PowerShellを使えばGUI操作に頼らずOS情報を自動取得・管理できます。用途に応じて、簡単な確認は [System.Environment]::OSVersion、詳細確認は Get-ComputerInfo、確実なスクリプト用途にはレジストリ参照、互換性を重視するならWMI/CIMと使い分けるのが有効です。管理者だけでなく開発者にとっても、環境依存の不具合を切り分ける際に役立つ知識です。

参考リンク

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?