PowerShellでOSやCPUを判定する方法
アプリをインストールする場合、インストーラーが32bitや64bitに分かれている場合があります。このようなアプリをPowerShellで自動でインストールするスクリプトを書く場合、実行したPCのCPUの種類を判定し、適切なインストーラーを実行する必要があります。
ここでは、PowerShellでCPUを判別する方法を記載します。
スクリプト
まずは、CPUを判定するスクリプト全体を記載します。
$os = Get-WmiObject -Class Win32_OperatingSystem
$os.OSarchitecture -match "64"
$os.OSarchitecture -match "32"
スクリプトの解説
まずGet-WmiObject -Class Win32_OperatingSystem
でWin32_OperatingSystemクラスのオブジェクトを取得しています。
このWin32_OperatingSystemにはOSの詳細な情報が格納されており、この中のOSarchitectureプロパティで64bit OSか32bit OSかを判定しています。
もし、64bit OSなら2行目がtrue、3行目がfalseとなります。32bit OSなら2行目がfalse、3行目がtrueとなります。
OSの判定方法
サンプルのスクリプトではCPUの判定だけでしたが、Win32_OperatingSystemの他のプロパティを使用すると、実行中のOSのバージョンを判定することができます。
以下にWin32_OperatingSystemの主なプロパティを記載します。
Win32_OperatingSystemのプロパティ一覧
プロパティ | データ型 | アクセス権 | 内容 |
---|---|---|---|
BuildNumber | string | 読取専用 | ビルド番号(例:19025) |
CSDVersion | string | 読取専用 | インストールされているサービスパック |
Caption | string | 読取専用 | OSの名称(例:Microsoft Windows 10 pro) |
CSName | string | 読取専用 | コンピュータ名(例:DESKTOP-12345) |
FreePhysicalMemory | uint64 | 読取専用 | 物理メモリの空き容量(KB) |
FreeVirtualMemory | uint64 | 読取専用 | 仮想メモリの空き容量(KB) |
InstallDate | datetime | 読取専用 | OSがインストールされた日時(例:2019/11/26 00:00) |
LastBootUpTime | datetime | 読取専用 | OSが最後に起動した日時(例:2019/11/26 00:00) |
ProductType | uint32 | 読取専用 | 追加のシステム情報(例:1)※1=ワークステーション、2=サーバー |
RegisteredUser | string | 読取専用 | ユーザー名 |
SerialNumber | string | 読取専用 | シリアルナンバー(例:10497-OEM-0031416-71674) |
ServicePackMajorVersion | uint16 | 読取専用 | 0 |
ServicePackMinorVersion | uint16 | 読取専用 | 0 |
SizeStoredInPagingFiles | uint64 | 読取専用 | ページングファイルのサイズ(KB) |
SystemDevice | string | 読取専用 | OSがインストールされている物理ディスクパーティション |
SystemDirectory | string | 読取専用 | OSのシステムディレクトリ |
SystemDrive | string | 読取専用 | OSのシステムドライブ |
TotalVirtualMemorySize | uint64 | 読取専用 | 仮想メモリの搭載容量(KB) |
TotalVisibleMemorySize | uint64 | 読取専用 | 物理メモリの搭載容量(KB) |
Version | string | 読取専用 | OSのバージョン(例:10.0.19025) |
WindowsDirectory | string | 読取専用 | Windowsのディレクトリ |
OSのバージョン一覧
Win32_OperatingSystemのCaptionやVersionプロパティを使用すれば実行中のOSの判断も可能です。ただ、バージョンの番号だけだと、どのアップデートかわからないため以下の一覧を参考にしてください。(すべてのOSで確認したわけではないので、間違っていたらご指摘いただけると嬉しいです。)
OS | Win10バージョン | Win32_OperatingSystem.Version |
---|---|---|
Windows 10 Nomember 2019 Update | 1903(19H2) | 10.0.18363 |
Windows 10 May 2019 Update | 1903(19H1) | 10.0.18362 |
Windows 10 October 2018 Update | 1809 | 10.0.17763 |
Windows 10 April 2018 Update | 1803 | 10.0.17134 |
Windows 10 Fall Creators Update | 1709 | 10.0.16299 |
Windows 10 Creators Update | 1703 | 10.0.15063 |
Windows 10 Anniversary Update | 1607 | 10.0.14393 |
Windows 10 November Update | 1511 | 10.0.10586 |
Windows 10 | 10.0 | 10.0.10240 |
Windows 8.1 | - | 6.3.9600 |
Windows 8 | - | 6.2.9200 |
Windows 7 SP1 | - | 6.1.7601 |
Windows 7 | - | 6.1.7600 |
Windows Vista SP2 | - | 6.0.6002 |
Windows Vista SP1 | - | 6.0.6001 |
Windows Vista | - | 6.0.6000 |
Server OS | Win32_OperatingSystem.Version |
---|---|
Windows Server 2019 | 10.0.17763 |
Windows Server version 1803 | 10.0.17134 |
Windows Server version 1709 | 10.0.16299 |
Windows Server 2016 | 10.0.14393 |
Windows Server 2012 R2 | 6.3.9600 |
Windows Server 2012 | 6.2.9200 |
Windows Server 2008 R2 SP1 | 6.1.7601 |
Windows Server 2008 R2 | 6.1.7600 |
Windows Server 2008 SP2 | 6.0.6002 |
Windows Server 2008 SP1 | 6.0.6001 |
Win32_OperatingSystemはVista以降のため、それ以前は参考までに、バージョンとビルド番号を記載しておきます。
OS | バージョン | ビルド番号 |
---|---|---|
Windows XP 64-Bit Edition | 5.2 | 3790 |
Windows XP | 5.1 | 2600 |
Windows 2000 | 5.0 | 2195 |
Windows NT 4.0 | 1381 | 1381 |
Windows ME | 4.9 | 3000 |
Windows 98 SE | 4.1 | 2222 |
Windows NT 3.51 | 3.51 | 1057 |
Windows NT 3.5 | 3.5 | 807 |
Windows NT 3.1 | 3.1 | 528 |
Windows 98 | 4.1 | 1998 |
Windows 95 OSR2.5 | 4.0 | 950C(1214) |
Windows 95 OSR2.1 | 4.0 | 950B(1212~1214) |
Windows 95 OSR2 | 4.0 | 950B(1111) |
Windows 95 OSR1 | 4.0 | 950a |
Windows 95 SP1 | 4.0 | 950a |
Windows 95 | 4.0 | 950 |
Windows 3.1 | 3.1 | - |
Server OS | バージョン | ビルド番号 |
---|---|---|
Windows Server 2003 R2 | 5.2 | 3790 |
Windows Server 2003 | 5.2 | 3790 |
Windows 2000 Server | 5.0 | 2195 |
Windows NT Server 4.0 | 1381 | 1381 |
Windows NT Server 3.51 | 3.51 | 1057 |
Windows NT Server 3.5 | 3.5 | 807 |
Windows NT 3.1 Advanced Server | 3.1 | 528 |
参考サイト
- Win32_OperatingSystem class- Win32 apps | Microsoft Docs
- Win32_OperatingSystemクラスのメンバーを確認できます。
最後まで読んでいただきありがとうございます。
また読んでくださいませ。
そんじゃーね。