Managing Disk Partitions

Microsoft® Windows® 2000 Scripting Guide

A partition is a structural division of a physical disk drive. Although a drive can contain a single partition, larger volumes are often divided into multiple partitions. This is why you might have drives C, D, and E even though your computer has only a single physical hard disk.

The Windows 2000 operating system supports the following partition types:

  • Primary partition. This is the only type of partition that can have an operating system installed. Each drive can have as many as four primary partitions, each assigned a different drive letter.

  • Extended partition. An additional partition that can be subdivided into multiple logical drives, each assigned a unique drive letter. A drive can have only one extended partition; however, you can divide this partition into multiple logical drives. This enables a disk to have more than just the four allowed primary partitions.

  • System partition. Any primary partition containing an operating system.

Partitions can tell you how a physical disk drive is actually being used. By examining the physical partitions on a disk, you can determine the following types of things:

  • How the disk has been divided into logical drives.

  • If there is unpartitioned space available on the disk. This can be determined by subtracting the size of all the partitions on a disk from the size of the disk itself.

  • If you can boot the computer from that disk (that is, does the disk contain a boot partition).

All these questions can be resolved by using the Win32_DiskPartition class. A subset of partition properties available through this class is shown in Table 10.2.

Table 10.2 Win32_DiskPartition Properties

Property

Description

BlockSize

Size (in bytes) of the blocks that form this partition.

Bootable

Boolean value indicating whether the computer can be booted from this partition.

BootPartition

Boolean value that indicates whether the partition is the active partition. The operating system uses the active partition when booting from a hard disk.

DeviceID

Unique identifier that differentiates the disk drive and partition from the rest of the system.

DiskIndex

Index number of the disk containing this partition.

Index

Index number of the partition.

Name

Label by which the object is known.

NumberOfBlocks

Total number of consecutive blocks, each block being the size of the value contained in the BlockSize property. The total size of the disk can be calculated by multiplying the value of the BlockSize property by the value of this property. If the value of BlockSize is 1, this property is the total size of the disk.

PrimaryPartition

Boolean value indicating whether this is the primary partition on the computer.

Size

Total size of the partition in bytes.

StartingOffset

Starting offset (in bytes) of the partition.

SystemName

Name of the computer where the partition is installed.

Type

Type of the partition. Values are:

  • Unused

  • 12-bit FAT

  • Xenix Type 1

  • Xenix Type 2

  • 16-bit FAT

  • Extended Partition

  • MS-DOS® V4 Huge

  • Installable File System

  • PowerPC Reference Platform

  • UNIX

  • NTFS file system

  • Win95 with Extended Int 13

  • Extended with Extended Int 13

  • Logical Disk Manager

  • Unknown

Scripting Steps

Listing 10.2 contains a script that enumerates the properties of all the disk partitions on a computer. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

  3. Use the ExecQuery method to query the Win32_DiskPartition class.

    This query returns a collection consisting of all the disk partitions on all the disk drives installed on the computer.

  4. For each disk partition in the collection, echo the value of properties such as the DeviceID, the partition size, and whether the partition is bootable.

Listing 10.2 Enumerating Disk Partition Properties

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDiskPartitions = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_DiskPartition")
For each objPartition in colDiskPartitions
 Wscript.Echo "Block Size: " & objPartition.BlockSize 
 Wscript.Echo "Bootable: " & objPartition.Bootable 
 Wscript.Echo "Boot Partition: " & objPartition.BootPartition
 Wscript.Echo "Description: " & objPartition.Description
 Wscript.Echo "Device ID: " & objPartition.DeviceID 
 Wscript.Echo "Disk Index: " & objPartition.DiskIndex 
 Wscript.Echo "Index: " & objPartition.Index 
 Wscript.Echo "Name: " & objPartition.Name 
 Wscript.Echo "Number Of Blocks: " & _
 objPartition.NumberOfBlocks 
 Wscript.Echo "Primary Partition: " & _
 objPartition.PrimaryPartition 
 Wscript.Echo "Size: " & objPartition.Size 
 Wscript.Echo "Starting Offset: " & _
 objPartition.StartingOffset 
 Wscript.Echo "Type: " & objPartition.Type 
Next