使用 Powershell 检测 GPT 和 MBR 分区

发布于 2024-09-26 05:08:21 字数 40 浏览 9 评论 0原文

有没有办法用powershell判断磁盘是否有GPT或MBR分区?

Is there a way to tell if a disk has a GPT or an MBR partition with powershell?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一曲爱恨情仇 2024-10-03 05:08:21

如果您使用的是 Windows 8、Windows Server 2012 或更高版本,则可以使用存储 cmdlet 之一来检查这一点:

Get-Disk

此命令的输出格式如下:

PS C:\> Get-Disk

Number Friendly Name                            OperationalStatus                    Total Size Partition Style
------ -------------                            -----------------                    ---------- ---------------
0      Microsoft Virtual Disk                   Online                                    42 GB GPT
1      Microsoft Virtual Disk                   Online                                     1 GB GPT
2      Microsoft Virtual Disk                   Offline                                    2 GB RAW
3      Microsoft Virtual Disk                   Offline                                    3 GB RAW

请注意,最右边的列指示分区样式,即分区样式。您正在寻找的数据。

如果您使用的是 Windows 7、Windows Server 2008 R2 或更早版本,则应使用 diskpart 或 WMI 来获取此信息。我更喜欢使用diskpart。输入

diskpart

后跟

list disk

输出将如下所示:

PS C:\> diskpart

Microsoft DiskPart version 6.3.9600

Copyright (C) 1999-2013 Microsoft Corporation.
On computer: WIN-BN8G3VMNQ9T

DISKPART> list disk

  Disk ###  Status         Size     Free     Dyn  Gpt
  --------  -------------  -------  -------  ---  ---
  Disk 0    Online           42 GB      0 B        *
  Disk 1    Online         1024 MB   991 MB        *
  Disk 2    Offline        2048 MB  2048 MB
  Disk 3    Offline        3072 MB  3072 MB

请注意,磁盘 0 和 1 都是 GPT 磁盘,并且它们在相应的列中有一个星号。

If you are on Windows 8, Windows Server 2012, or newer, then you can use one of the storage cmdlets to check this:

Get-Disk

The output of this command will be formatted like:

PS C:\> Get-Disk

Number Friendly Name                            OperationalStatus                    Total Size Partition Style
------ -------------                            -----------------                    ---------- ---------------
0      Microsoft Virtual Disk                   Online                                    42 GB GPT
1      Microsoft Virtual Disk                   Online                                     1 GB GPT
2      Microsoft Virtual Disk                   Offline                                    2 GB RAW
3      Microsoft Virtual Disk                   Offline                                    3 GB RAW

Notice that the rightmost column indicates the Partition Style, which is the piece of data that you are seeking.

If you are on Windows 7, Windows Server 2008 R2, or older, then you should use diskpart or WMI to get this information. I prefer to use diskpart. Type

diskpart

followed by

list disk

The output will look like:

PS C:\> diskpart

Microsoft DiskPart version 6.3.9600

Copyright (C) 1999-2013 Microsoft Corporation.
On computer: WIN-BN8G3VMNQ9T

DISKPART> list disk

  Disk ###  Status         Size     Free     Dyn  Gpt
  --------  -------------  -------  -------  ---  ---
  Disk 0    Online           42 GB      0 B        *
  Disk 1    Online         1024 MB   991 MB        *
  Disk 2    Offline        2048 MB  2048 MB
  Disk 3    Offline        3072 MB  3072 MB

Note that Disk 0 and 1 are both GPT disks, and they have an asterisk in the appropriate column.

帅气尐潴 2024-10-03 05:08:21

使用 WMI

Get-WmiObject -Query "Select * from Win32_DiskPartition WHERE Index = 0" |
  Select-Object DiskIndex, @{
    Name = "GPT";
    Expression = {$_.Type.StartsWith("GPT")}
  }

使用 Diskpart

$a = "list disk" | diskpart

$m = [String]::Join("`n", $a) |
  Select-String -Pattern "Disk (\d+).{43}(.)" -AllMatches

$m.Matches |
  Select-Object @{
    Name = "DiskIndex";
    Expression = {$_.Groups[1].Value}}, @{
    Name = "GPT";
    Expression = {$_.Groups[2].Value -eq "*"}
  }

Using WMI

Get-WmiObject -Query "Select * from Win32_DiskPartition WHERE Index = 0" |
  Select-Object DiskIndex, @{
    Name = "GPT";
    Expression = {$_.Type.StartsWith("GPT")}
  }

Using Diskpart

$a = "list disk" | diskpart

$m = [String]::Join("`n", $a) |
  Select-String -Pattern "Disk (\d+).{43}(.)" -AllMatches

$m.Matches |
  Select-Object @{
    Name = "DiskIndex";
    Expression = {$_.Groups[1].Value}}, @{
    Name = "GPT";
    Expression = {$_.Groups[2].Value -eq "*"}
  }

韬韬不绝 2024-10-03 05:08:21

要查明任何磁盘是否有 MBR 或 GPT,这非常简单。
启动powershell。跑步..
diskpart ,按 ENTER 键,运行..
列出磁盘,按 ENTER 键。这是我的计算机的输出:

Disk ###  Status         Size     Free     Dyn  Gpt
--------  -------------  -------  -------  ---  ---
Disk 0    Online          232 GB  1024 KB
Disk 1    Online          465 GB  1024 KB
Disk 2    Online         3820 MB      0 B

如果您的磁盘是动态的或者分区表类型分别是 Gpt,您将得到一个 yes 条目,我认为!,对于 Dyn 或 Gpt。
我搜索了一段时间,对于我的目的来说已经足够了。为了比较 Josh 使用 WMI 的脚本输出,以下是输出。

DiskIndex                                     GPT
---------                                     ---
        0                                   False
        1                                   False
        2                                   False

To find out if any disk has a MBR or a GPT this is very easy..
Start powershell. Run..
diskpart , press ENTER, run..
list disk , press ENTER. Here is my computer's output:

Disk ###  Status         Size     Free     Dyn  Gpt
--------  -------------  -------  -------  ---  ---
Disk 0    Online          232 GB  1024 KB
Disk 1    Online          465 GB  1024 KB
Disk 2    Online         3820 MB      0 B

You will get a yes entry, I think!, for Dyn or Gpt if your disk is dynamic or the partition table type is Gpt respectively.
I searched for some time and enough is enough for my purposes. To compare the output of the script by Josh using WMI, here is the output..

DiskIndex                                     GPT
---------                                     ---
        0                                   False
        1                                   False
        2                                   False
空宴 2024-10-03 05:08:21

否。PowerShell 没有任何用于此目的的本机内置命令。 PowerShell,顾名思义,是一个shell。它附带了一组很好的有用的通用 cmdlet,但像这样的专业化留给了外部本机命令(如 diskpart)、模块和/或管理单元。

由于您总是会在找到 powershell 的地方找到 diskpart.exe,因此请使用它。

如果您打算单独使用 PowerShell,那么 WMI 或许可以提供帮助。看一下输出:

PS>; gwmi win32_分区 | % { $_ | fl * }

-Oisin

No. PowerShell does not have any native built-in commands for this. PowerShell, as the name suggests, is a shell. It comes with a good set of useful, generic cmdlets but specialization like this is left to external native commands (like diskpart), modules and/or snapins.

Since you're always going to find diskpart.exe where you find powershell, use that.

If you're intent on using PowerShell alone, then perhaps WMI could help. Take a look at the output of:

PS> gwmi win32_partition | % { $_ | fl * }

-Oisin

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文