如何从PowerShell命令行查找Windows版本?

jra*_*ara 106 windows powershell powershell-2.0

如何找到我正在使用的Windows版本?

我正在使用PowerShell 2.0并尝试:

PS C:\> ver
The term 'ver' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify tha
t the path is correct and try again.
At line:1 char:4
+ ver <<<< 
    + CategoryInfo          : ObjectNotFound: (ver:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

Jef*_*ado 148

由于您可以访问.NET库,因此可以访问该类的OSVersion属性System.Environment以获取此信息.对于版本号,Version有财产.

例如,

PS C:\> [System.Environment]::OSVersion.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
6      1      7601   65536
Run Code Online (Sandbox Code Playgroud)

可以在此处找到Windows版本的详细信息.

  • @CMCDragonkai `(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId` (4认同)
  • 注意`[Environment] :: OSVersion`在[tag:windows-10]中工作,`OSVersion.Version.Major`返回10. (3认同)
  • 当我运行`winver`时,它显示的是版本1607。但是上面的powershell命令没有给出1607。在Powershell中,哪里可以得到这个“ 1607”数字? (2认同)
  • 自Windows 8.1起,此方法已弃用.有关详细信息,请参阅[link](https://blogs.technet.microsoft.com/heyscriptingguy/2014/04/25/use-powershell-to-find-operating-system-version/). (2认同)
  • @SlogmeisterExtraordinaire 命令 `[System.Environment]::OSVersion` 并未被弃用,它在后台使用的方法已被弃用。新的 PS 版本正在更改后端行为:https://github.com/PowerShell/PowerShell/issues/2009#issuecomment-288516808 (2认同)

Ric*_*ard 100

  1. 要获得Windows版本号,正如杰夫在他的回答中所说,使用:

    [Environment]::OSVersion
    
    Run Code Online (Sandbox Code Playgroud)

    值得注意的是,结果是类型的[System.Version],因此可以检查,例如,Windows 7/Windows Server 2008 R2及更高版本

    [Environment]::OSVersion.Version -ge (new-object 'Version' 6,1)
    
    Run Code Online (Sandbox Code Playgroud)

    但是,这不会告诉您它是客户端还是服务器Windows,也不会告诉您版本的名称.

  2. 使用WMI的Win32_OperatingSystem类(总是单个实例),例如:

    (Get-WmiObject -class Win32_OperatingSystem).Caption
    
    Run Code Online (Sandbox Code Playgroud)

    将返回类似的东西

    Microsoft®WindowsServer®2008标准版


Ant*_*lov 39

不幸的是,大多数其他答案都没有提供Windows 10特有的信息.

Windows 10有自己的版本:1507,1511,1607,1703等.这是winver显示的.

Powershell:
(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId

Command prompt (CMD.EXE):
Reg Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ReleaseId
Run Code Online (Sandbox Code Playgroud)

另请参阅有关超级用户的相关问题.

至于其他Windows版本使用systeminfo.Powershell包装:

PS C:\> systeminfo /fo csv | ConvertFrom-Csv | select OS*, System*, Hotfix* | Format-List


OS Name             : Microsoft Windows 7 Enterprise
OS Version          : 6.1.7601 Service Pack 1 Build 7601
OS Manufacturer     : Microsoft Corporation
OS Configuration    : Standalone Workstation
OS Build Type       : Multiprocessor Free
System Type         : x64-based PC
System Locale       : ru;Russian
Hotfix(s)           : 274 Hotfix(s) Installed.,[01]: KB2849697,[02]: KB2849697,[03]:...
Run Code Online (Sandbox Code Playgroud)

Windows 10输出相同的命令:

OS Name             : Microsoft Windows 10 Enterprise N 2016 LTSB
OS Version          : 10.0.14393 N/A Build 14393
OS Manufacturer     : Microsoft Corporation
OS Configuration    : Standalone Workstation
OS Build Type       : Multiprocessor Free
System Type         : x64-based PC
System Directory    : C:\Windows\system32
System Locale       : en-us;English (United States)
Hotfix(s)           : N/A
Run Code Online (Sandbox Code Playgroud)

  • 这很容易记住桌面上的“winver”和服务器上的“systeminfo”。多年来一直让我困惑的是,没有统一的方式来获取这些信息。 (3认同)
  • releaseid 不再适用于 21h1,而是现在使用 displayversion。旧版本不存在 (3认同)
  • 指向实际有用的 MS 信息的重要链接。应该注意的是,对于 Win8.1(及以下?),显示的信息是:`OS Version : 6.3.9600 N/A Build 9600`。因此,在 W81 以下的版本中,查看(总是被忽视的)LTSB 版本可能会提供更多信息。请参阅以下输出:`(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").BuildLabEx` 可能类似于:`9600.19179.amd64fre.winblue_ltsb_escrow.181015-1847`。我的**猜测**是 `181015` 部分是构建日期,而 `1847` 是构建或发布版本。您可能还需要将其与 *kernel、hal* 进行比较。 (2认同)
  • 我使用的是 Windows 11,并认为上面注册表中的“BuildLabEx”就是它,但实际上它仍然报告一年前的(RTM?)版本 22000.1 + 实验室字符串,而不是根据 winver 的我当前的版本 22000.675。“CurrentVersion”键中没有任何内容将我当前的内部版本号报告为完整字符串的一部分。只有值“UBR”才会报告正确的整数 675,但不是完整构建字符串的形式。 (2认同)

小智 24

Get-WmiObject -Class Win32_OperatingSystem | ForEach-Object -MemberName Caption
Run Code Online (Sandbox Code Playgroud)

或者打高尔夫球

gwmi win32_operatingsystem | % caption
Run Code Online (Sandbox Code Playgroud)

结果

Microsoft Windows 7 Ultimate

  • 建议在新代码中使用Get-CimInstance而不是Get-WmiObject. (4认同)

Iho*_*ich 17

这将为您提供完整版本的Windows(包括版本/内部版本号),与上述所有解决方案不同:

(Get-ItemProperty -Path c:\windows\system32\hal.dll).VersionInfo.FileVersion
Run Code Online (Sandbox Code Playgroud)

结果:

10.0.10240.16392 (th1_st1.150716-1608)
Run Code Online (Sandbox Code Playgroud)

  • 就我而言,这是最好的解决方案,因为它正确地报告了修订号.没有其他人(至少我已经测试过). (5认同)
  • 到目前为止,这是唯一允许我获得完整版本号的解决方案.*但是*,并不是每次更新都会更新system32中的所有文件 - 例如,我的hal.dll仍然显示为"10.0.10586.0(th2_release.151029-1700)",而winload.exe则为"10.0.10586.63"(th2_release. 160104-1513)`. (5认同)
  • 这依赖于微软方面的实施细节,不能保证他们会继续这样做.它现在可以正常工作,但如果你希望你的脚本长期工作,你应该避免依赖它. (5认同)
  • 这是一个小脚本,它从具有最高构建日期的dll/exe获取版本:[gist](https://gist.github.com/melak47/f58d4d2d52b0a8d976b4) (2认同)

小智 12

从PowerShell 5开始:

Get-ComputerInfo
Get-ComputerInfo -Property Windows*
Run Code Online (Sandbox Code Playgroud)

我认为这个命令几乎尝试了迄今为止发现的1001种收集系统信息的方法......


Lar*_*dal 9

PS C:\> Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer
Run Code Online (Sandbox Code Playgroud)

退货

WindowsProductName    WindowsVersion OsHardwareAbstractionLayer
------------------    -------------- --------------------------
Windows 10 Enterprise 1709           10.0.16299.371 
Run Code Online (Sandbox Code Playgroud)

  • 获取计算机信息 | select WindowsVersion 不再适用于 21h1 - 它显示 2009(与 20h2 相同) (3认同)

Moo*_*tom 8

如果要区分Windows 8.1(6.3.9600)和Windows 8(6.2.9200)使用

(Get-CimInstance Win32_OperatingSystem).Version 
Run Code Online (Sandbox Code Playgroud)

获得正确的版本.[Environment]::OSVersion在Windows 8.1中无法正常工作(它返回Windows 8版本).


Far*_*res 8

我正在完善其中一个答案

我在尝试匹配winver.exe的输出时遇到了这个问题:

Version 1607 (OS Build 14393.351)

我能够用以下内容提取构建字符串:

,((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name BuildLabEx).BuildLabEx -split '\.') | % {  $_[0..1] -join '.' }  
Run Code Online (Sandbox Code Playgroud)

结果: 14393.351

更新:这是一个使用正则表达式的略微简化的脚本

(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").BuildLabEx -match '^[0-9]+\.[0-9]+' |  % { $matches.Values }
Run Code Online (Sandbox Code Playgroud)


Mic*_*yce 8

如果您尝试破译 MS 在其修补网站上发布的信息,例如https://technet.microsoft.com/en-us/library/security/ms17-010.aspx

您将需要一个组合,例如:

$name=(Get-WmiObject Win32_OperatingSystem).caption $bit=(Get-WmiObject Win32_OperatingSystem).OSArchitecture $ver=(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId Write-Host $name, $bit, $ver

微软 Windows 10 家庭版 64 位 1703


Edd*_*mar 7

除了其他答案之外,以下是一些可以使用 PowerShell 检索的有用信息:

\n

通过PowerShell查询操作系统和硬件信息:

\n

查询通用OS(操作系统)信息:

\n

查看操作系统名称的最快方法:

\n
cmd ?\n
Run Code Online (Sandbox Code Playgroud)\n

#使用获取计算机信息:

\n
Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer\n
Run Code Online (Sandbox Code Playgroud)\n

#使用 Get-WmiObject:

\n
$name=(Get-WmiObject Win32_OperatingSystem).caption\n$bit=(Get-WmiObject Win32_OperatingSystem).OSArchitecture\n$ver=(Get-ItemProperty "HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion").ReleaseId\nWrite-Host " OS-Name: `t $name `n Architct: `t $bit  `n Release: `t $ver" \n
Run Code Online (Sandbox Code Playgroud)\n

列出主要次要版本信息:

\n
[System.Environment]::OSVersion.Version \n
Run Code Online (Sandbox Code Playgroud)\n

查询主机名:

\n
$Env:ComputerName\n
Run Code Online (Sandbox Code Playgroud)\n

或者

\n
hostname    #cmd command\n
Run Code Online (Sandbox Code Playgroud)\n

另外,如果您知道 IP 地址,请使用“ping”命令(例如:),ping /a <your_ip_address>您将在第一行看到您的“主机名”。

\n

查询当前(登录)用户:

\n
whoami    #cmd command\n
Run Code Online (Sandbox Code Playgroud)\n

或者

\n
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name \n
Run Code Online (Sandbox Code Playgroud)\n

查询映射驱动器: \n列出映射驱动器 - 使用 WMI:

\n
Get-WmiObject -Class Win32_LogicalDisk | Format-Table \n
Run Code Online (Sandbox Code Playgroud)\n

或者

\n
wmic logicaldisk get name       #list just logical-drive letters\n
Run Code Online (Sandbox Code Playgroud)\n

或者,列出逻辑驱动器信息:FreeSpace、Provider(真实网络位置)、Size 和 VolumeName:

\n
wmic logicaldisk list brief\n
Run Code Online (Sandbox Code Playgroud)\n

列出映射驱动器 - 使用 [DriveInfo] 类:

\n
[System.IO.DriveInfo]::GetDrives()\n
Run Code Online (Sandbox Code Playgroud)\n

列出可移动驱动器:

\n
$drives = [System.IO.DriveInfo]::GetDrives()\n$r = $drives | Where-Object { $_.DriveType -eq 'Removable' -and $_.IsReady }\nif ($r) {\n    return @($r)[-1]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

查询磁盘容量、空间和卷类型

\n
Invoke-Command -ComputerName S1 {Get-PSDrive C} | Select-Object PSComputerName,Used,Free \n
Run Code Online (Sandbox Code Playgroud)\n

可用空间:

\n
(Get-PSDrive C).Free\n
Run Code Online (Sandbox Code Playgroud)\n

或(以 GB 为单位)

\n
[Math]::Floor(((Get-PSDrive C).Free /[Math]::Pow(2, 30)*10)) /10\n
Run Code Online (Sandbox Code Playgroud)\n

已用空间:

\n
(Get-PSDrive C).Used\n
Run Code Online (Sandbox Code Playgroud)\n

OR(已用空间,以 GB 为单位)

\n
[Math]::Floor(((Get-PSDrive C).Used /[Math]::Pow(2, 30)*10)) /10\n
Run Code Online (Sandbox Code Playgroud)\n

另外要查看总空间:(以 GB 为单位)

\n
$totalSpace = ((Get-PSDrive C).Used + (Get-PSDrive C).Free)/(1024*1024*1024)\nOR\n$totalSpace = ((Get-PSDrive C).Used + (Get-PSDrive C).Free)/[Math]::Pow(2, 30)\n
Run Code Online (Sandbox Code Playgroud)\n

四舍五入值:

\n
[Math]::Floor($totalSpace*10) / 10\nOR\n[Math]::Round($totalSpace,1)\n
Run Code Online (Sandbox Code Playgroud)\n

查询主板信息:

\n
wmic baseboard get product,Manufacturer,version,serialnumber\n
Run Code Online (Sandbox Code Playgroud)\n

查询磁盘卷(磁盘分区)信息: \nGet-Volume 返回有关存储驱动器分区的信息,例如:

\n
Get-Volume                 # All partitions\nGet-Volume -DriveLetter C  # Specific partition\n
Run Code Online (Sandbox Code Playgroud)\n

#文件系统类型:

\n
Get-Volume -DriveLetter C | select FileSystem\n(Get-Volume -DriveLetter C).FileSystem\n
Run Code Online (Sandbox Code Playgroud)\n

#分区大小:

\n
Get-Volume -DriveLetter C | select Size\nOR (in GB)\n[Math]::Floor(((Get-Volume -DriveLetter C).Size/[Math]::Pow(2, 30)*10)) /10\n
Run Code Online (Sandbox Code Playgroud)\n

查询内存/查询 RAM

\n
Get-WmiObject Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum\nOR (in GB)\n$memory = (Get-WmiObject Win32_PhysicalMemory | Measure -Property Capacity -Sum).Sum\n$memory = [Math]::Floor(($memory/[Math]::Pow(2, 30)*10)) /10\n$memory.ToString() + " gb"\n
Run Code Online (Sandbox Code Playgroud)\n

#查询RAM包括频率/速度:

\n
Get-CimInstance win32_physicalmemory | Format-Table Manufacturer,Banklabel,Configuredclockspeed,Devicelocator,Capacity,Serialnumber \xe2\x80\x93autosize\n
Run Code Online (Sandbox Code Playgroud)\n
\n

如前所述,这个答案有点超出了所提出的问题,但对于那些想要使用 PowerShell 获取其他操作系统或硬件信息的人来说可能很有用。

\n


小智 5

我接受了上面的脚本,并对它们进行了一些微调,以得出以下结论:

$name=(Get-WmiObject Win32_OperatingSystem).caption
$bit=(Get-WmiObject Win32_OperatingSystem).OSArchitecture

$vert = " Version:"
$ver=(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId

$buildt = " Build:"
$build= (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").BuildLabEx -match '^[0-9]+\.[0-9]+' |  % { $matches.Values }

$installd = Get-ComputerInfo -Property WindowsInstallDateFromRegistry

Write-host $installd
Write-Host $name, $bit, $vert, $ver, `enter code here`$buildt, $build, $installd
Run Code Online (Sandbox Code Playgroud)

要获得这样的结果:

Microsoft Windows 10 Home 64位版本:1709内部版本:16299.431 @ {WindowsInstallDateFromRegistry = 18-01-01 2:29:11 AM}

提示:我希望能从安装日期中删除前缀文本,以便可以用可读性更高的标题替换它。


小智 5

要在 Windows 10 1809 上的 PowerShell v5 中生成与 winver.exe 相同的输出:

$Version = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\'
"Version $($Version.ReleaseId) (OS Build $($Version.CurrentBuildNumber).$($Version.UBR))"
Run Code Online (Sandbox Code Playgroud)