Managing Disk Quotas in Windows Server 2003 and Windows XP

An overview of the new features found in Windows Server 2003 and Windows XP that let you to write scripts that manage disk quotas and disk quota settings across the enterprise.

*
On This Page
A Brief History of Disk QuotasA Brief History of Disk Quotas
How Disk Quotas WorkHow Disk Quotas Work
Settings for Managing Disk QuotasSettings for Managing Disk Quotas
Enabling and Disabling Disk QuotasEnabling and Disabling Disk Quotas
Modifying Disk Quota SettingsModifying Disk Quota Settings
Managing Disk Quota Entries for Individual UsersManaging Disk Quota Entries for Individual Users
Retrieving a Single Disk Quota EntryRetrieving a Single Disk Quota Entry
Modifying a Disk Quota EntryModifying a Disk Quota Entry
Deleting a Disk Quota EntryDeleting a Disk Quota Entry
Creating a New Disk Quota EntryCreating a New Disk Quota Entry
For More Information …For More Information …

A Brief History of Disk Quotas

Any time you hear an old adage it’s usually pretty easy to find at least a grain of truth in the advice. Look before you leap? That makes sense. A fool and his money are soon parted? Just a second while we check the Scripting Guys budget … OK, we can’t argue with that. The early bird gets the worm? Well, that one we’ll have to take on faith. But it sounds reasonable enough.

But then there’s this one: Be careful what you wish for, because you just might get it. What? That doesn’t make any sense. Why would you wish for something and then be unhappy when you got it? Oh, well: apparently even writers of old adages have their off days. (Which would go a long ways towards explaining the old saying, “You can’t have your cake and eat it, too.”)

As it turns out, though, system administrators who were around before Windows 2000 understand the meaning of that old “be careful what you wish for” saying very well. Back in the Windows NT era, one of the top items on any system administrator’s wish list was disk quotas, something Windows NT did not support. And guess what happened when Windows 2000 was released? That’s right, your wish was Microsoft’s command: Windows 2000 fully supported disk quotas (on NTFS volumes) right out of the box. As another saying goes, “Ask and ye shall receive.”

So what was the problem? Well, let’s make it clear that there was nothing wrong with the disk quotas themselves; they worked exactly as advertised. The problem was that system administrators now had a wonderful new tool at their disposal, but no really good way to manage this tool. Disk quotas had to be managed on a drive-by-drive basis; there was no easy way to get at overall disk quota use on a computer (that is, quota use spanning all the drives on a machine). Not only that, but:

There was no way to generate disk quota reports.

Managing disk quotas on remote machines, though possible, had to be done through slightly-obscure methods.

The scripting options were equally limited, being designed to work on a single drive, and on only the local computer. (Incidentally, if you’re interesting in using scripts to manage disk quotas on Windows 2000, please see “Managing Disk Quotas” in the Microsoft Windows 2000 Scripting Guide.)

In other words, system administrators got exactly what they wished for: disk quotas. The problem was that they hadn’t wished for disk quota management tools at the same time. Disk quotas were – and still are – very useful in Windows 2000, but the dearth of management tools makes it very difficult to administer disk quotas across the enterprise.

But you know what they say: good things come to those who wait. That seems to be the case with disk quotas. With Windows 2000, system administrators got disk quotas, but didn’t get much help when it came to managing quota use across the enterprise. With Windows Server 2003 (and Windows XP), that’s changed. Now you can easily manage disk quotas across the enterprise. That’s because Windows Server 2003 and Windows XP include two new WMI classes designed for managing disk quotas and disk quota settings.

This article will detail these two WMI classes and offer a number of sample scripts that show you how to use those classes to carry out all sorts of management tasks. Before we do that, however, we need to take a brief timeout and talk a little bit about the technology behind disk quotas.

Top of pageTop of page

How Disk Quotas Work

Disk quotas are an integral part of the NTFS file system. Any time a file (or folder) is created on an NTFS volume, that file or folder is assigned an owner (typically the user who created the file or folder in the first place). Before the file is actually saved, NTFS obtains the user ID of the file owner and stores that information in the file’s Standard Information attribute (which is used when tallying the disk space allocation). NTFS then locates the disk quota entry for that user and determines whether the new allocation of disk space will cause the user to exceed the assigned quota. If it will, NTFS then takes the appropriate steps, which can include logging an entry in the System event log or preventing the user from creating the file or folder. As the file changes size, NTFS updates the disk quota entry to reflect the total disk space used by the user. All this happens behind the scenes, and so quickly that you aren’t even aware of it.

Before you can effectively manage disk quotas it’s important to understand that quotas are not configured on a computer-wide basis. Instead, disk quotas are tied to individual NTFS volumes. Each drive has separate quota settings, and the actions you take on one volume do not affect the other volumes. For example, a computer might have a single hard disk divided into three volumes: drives C, D, and E. Each of these drives will have separate quota settings. You can enable disk quotas on drives C and D, yet disable them on drive E. Likewise, you can grant users 50 megabytes of disk space on drive C, and 100 megabytes of disk space on drive D.

In other words, when managing disk quotas the actions you take on one volume do not affect the other volumes in any way. If you allocate User A 50 megabytes of disk space on drive C, this does not also give User A 50 megabytes of disk space on drives D and E. If you disable disk quotas on drive D, the quotas remain enabled on drives C and E. And if you – well, you get the idea. Keep in mind, too, that disk quotas are also configured on a per-user basis, something we’ll discuss in more detail later in this article.

In addition to allocating disk space you can also specify a quota warning level, the amount of disk space use that triggers an alert (an event recorded in the System event log informing you that a user is nearing the quota). You can also determine the action the system takes if a user exceeds the quota:

Take no action at all.

Record an event in the System event log.

Deny the user the right to store additional data until he or she has removed enough files to get back under the quota limit.

WMI includes two classes used for managing disk quotas on NTFS drives. The Win32_QuotaSetting class is used to configure quota settings for each drive; this includes such things as whether or not quotas are enabled on the drive and the default quota and warning limits applied to new users. Meanwhile, the Win32_DiskQuota class is designed to manage individual quota entries: it is used to enumerate, create, modify, and delete quota entries for individual users. Using just these two classes you can perform pretty much any disk quota management task you can think of.

Top of pageTop of page

Settings for Managing Disk Quotas

For those of you who skipped the preceding educational section, we’ll reiterate: the Win32_QuotaSetting class is used to determine such things as whether or not quotas are enabled (for any or all drives on a computer); the default quota and warning limits configured for a drive; and the administrative actions, if any, that will be taken if a user exceeds his or her quota. Win32_QuotaSetting includes the properties shown in the following table. All of these properties – except VolumePath – are read/write, meaning you are not limited merely to reporting the existing value of these properties. Instead, you can use scripts to actually modify the value of these properties.

Property

Description

DefaultLimit

Default limit (in kilobytes) for quotas on this particular volume.

DefaultWarningLimit

Default warning limit (in kilobytes) for quotas on this particular volume.

ExceededNotification

Indicates whether or not events are written to the event log if users exceed their quotas. (True/False)

State

Level of quota management for this particular volume. Values are:
0: Quota management is not enabled on this volume.
1: Quotas are tracked but the limit value is not enforced and users can exceed their quotas.
2: Quotas are tracked and enforced on this volume.

VolumePath

Name of the volume containing the disk quotas. This can be the volume name or the volume path (such as D:\\). When using a path, the two backslashes (\\) are required.

WarningExceededNotification

Indicates whether or not events are written to the event log when the warning limit is exceeded. (True/False)

These same properties can also be mapped to the user interface elements found in the Properties dialog box for an NTFS drive:

Disk Quotas


To see as full-size version of this graphic, please click here.

So what can you actually do with Win32_QuotaSetting? Well, probably the most common task is simply to connect to a computer and find out whether or not disk quotas have been enabled on the computer’s NTFS volumes (and, if so, to determine how the quota settings have been configured). To return quota setting information for a computer all you have to do is bind to the Win32_QuotaSetting class and then echo back the values of each property:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colQuotaSettings = objWMIService.ExecQuery _
    ("Select * from Win32_QuotaSetting")

For Each objQuotaSetting in colQuotaSettings
    Wscript.Echo "Default Limit: " & objQuotaSetting.DefaultLimit
    Wscript.Echo "Default Warning Limit: " &  _
        objQuotaSetting.DefaultWarningLimit    
    Wscript.Echo "Exceeded Notification: " &  _
        objQuotaSetting.ExceededNotification   
    Wscript.Echo "State: " & objQuotaSetting.State    
    Wscript.Echo "Volume Path: " & objQuotaSetting.VolumePath
    Wscript.Echo "Warning Exceeded Notification: " & _
        objQuotaSetting.WarningExceededNotification    
Next

The preceding script returns quota setting information for all the NTFS volumes on a computer. Suppose you wanted information about quota settings only for drive D? In that case, simply add a WHERE clause to your WQL query, one that limits the returned data to drive D:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colQuotaSettings = objWMIService.ExecQuery _
    ("Select * from Win32_QuotaSetting Where VolumePath = 'D:\\'")

For Each objQuotaSetting in colQuotaSettings
    Wscript.Echo "Default Limit: " & objQuotaSetting.DefaultLimit
    Wscript.Echo "Default Warning Limit: " &  _
        objQuotaSetting.DefaultWarningLimit    
    Wscript.Echo "Exceeded Notification: " &  _
        objQuotaSetting.ExceededNotification   
    Wscript.Echo "State: " & objQuotaSetting.State    
    Wscript.Echo "Volume Path: " & objQuotaSetting.VolumePath
    Wscript.Echo "Warning Exceeded Notification: " & _
        objQuotaSetting.WarningExceededNotification    
Next

Note that you must include an extra slash when specifying drive D; it’s D:\\. Because the \ is a reserved character in WMI, it must be “escaped” (by prefacing it with another slash) any time it appears in a WHERE clause.

Top of pageTop of page

Enabling and Disabling Disk Quotas

Win32_QuotaSetting makes it easy to enable and disable disk quotas; using WMI scripts to perform these tasks is especially useful because they can be carried out on remote computers just as easily as on the local computer. Scripts are also a nice way to manage disk quotas because they give you the flexibility of enabling disk quotas for a single volume or for every volume on the computer. It’s entirely up to you.

You can turn disk quotas on and off by manipulating the value of the State property. State accepts three possible values:

0: Disk quotas are turned off.

1: Disk quotas are tracked but not enforced. In other words, the operating system will keep track of quota information, but users will be allowed to exceed their quota limit at will.

2: Disk quotas are tracked and enforced. In this case, users will not be allowed to exceed their disk quotas; a user attempting to save a file that would put them over their quota limit will be denied access due to insufficient disk space.

The following script configures a computer to track and enforce disk quotas for drive C on a computer:

Const ENFORCE_QUOTAS = 2

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_QuotaSetting Where VolumePath = 'C:\\'")

For Each objDisk in colDisks
    objDisk.State = ENFORCE_QUOTAS
    objDisk.Put_
Next

The script begins by defining a constant – ENFORCE_QUOTAS – and assigning it the value 2. The script connects to the WMI service, and then retrieves the quota settings for drive C by using this line of code:

Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_QuotaSetting Where VolumePath = 'C:\\'")

To enable quotas, the script sets the value of the State property to ENFORCE_QUOTAS and then calls the Put_ method to write this change to the operating system.

What if you want to disable disk quotas on a drive? In that case, you simply need to set the value of the State property to 0. To do that, create a constant named DISABLE_QUOTAS, set the value of that constant to 0, and then set the value of the State property to DISABLE_QUOTAS. Your finished script will look an awful lot like this:

Const DISABLE_QUOTAS = 0

StrComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_QuotaSetting Where VolumePath = 'C:\\'")

For Each objDisk in colDisks
    objDisk.State = DISABLE_QUOTAS
    objDisk.Put_
Next

Note that when you disable disk quotas, existing quota information is not discarded. For example, any quota settings you have made or any quota entries you have created will still exist. However, those settings will not be enforced, and disk space usage for each individual with a quota entry will not be updated. If you later re-enable disk quotas, those previous settings and quota entries will automatically be restored, you won’t have to re-create them. Waste not, want not as we Scripting Guys like to say.

Top of pageTop of page

Modifying Disk Quota Settings

One of the really nice things about the Win32_QuotaSetting class is the fact that most of the properties are read/write; in other words, not only can you use a script to retrieve the values of properties like DefaultLimit and DefaultWarningLimit, but you can also use a script to modify those property values.

For example, this script modifies the DefaultLimit and DefaultWarningLimit settings for all the NTFS drives on a computer:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colQuotaSettings = objWMIService.ExecQuery _
    ("Select * from Win32_QuotaSetting")

For Each objQuotaSetting in colQuotaSettings
    objQuotaSetting.DefaultLimit = 10000000
    objQuotaSetting.DefaultWarningLimit = 9000000
    objQuotaSetting.Put_
Next

As you can see, you simply assign a new value (in kilobytes) and then call the Put_ method to write those new quota settings to the operating system. Note that when you do this the revised quota settings apply only to new users; these new settings do not apply to users who already have disk quota entries on the drive. For example, suppose you have three users with disk quota entries on a drive:

User

Quota Limit

Warning Limit

KMyer

100 MB

90 MB

PAckerman

200 MB

180 MB

RWilliams

400 MB

350 MB

Suppose you run a script that changes the default quota settings to a quota limit of 50 megabytes and a warning limit of 40 megabytes. These values will be applied to any new user who saves a file or creates a folder on the drive. However, the quota settings for the three users shown in the preceding table will not be affected; they will maintain their existing settings even though they all have a quota limit well above the proscribed maximum of 50 megabytes. But that’s OK: after all, quota settings are a default value that will be applied to all new users unless you specify otherwise. In other words, it’s perfectly permissible to have individual users with quota settings higher or lower than the default values.

But what if you really do want to apply these same values to our three users? In that case, you have to change the values for each user’s individual quota entry. And that – fortuitously enough – leads us right into our next section.

Top of pageTop of page

Managing Disk Quota Entries for Individual Users

Each time a new user saves a file to a drive where disk quotas have been enabled a quota entry is created for that user. That quota entry keeps track of the quota and warning limits that have been assigned to the user as well as the amount of disk space currently charged to that user. In fact, the quota entry is remarkably similar to the WMI class Win32_DiskQuota, a class that can be used to create, delete, modify, and report on individual quota entries. Properties for the Win32_DiskQuota class are shown in the following table:

Property

Description

DiskSpaceUsed

Current number of kilobytes (KB) currently in use by this particular user.

Limit

Limit set for this particular user. This property is read/write.

QuotaVolume

Volume where the quota entry is stored.

Status

Current status of the disk quota. Values are:
0. OK: the user is within his or her quota limit.
1. Warning: the user has exceeded the warning limit, but not yet exceeded his or her quota limit.
2. Exceeded: the user has exceeded his or her quota limit.

User

User associated with the quota entry.

WarningLimit

Warning limit set for this particular user. This property is read/write.

These properties can also be mapped to the user interface elements found in the Quota Entries dialog box:

Disk Quotas


To see as full-size version of this graphic, please click here.

Note. How do you find the Quota Entries dialog box? In the My Computer folder, right-click the desired disk drive and then click Properties. In the Properties dialog box, click the Quota tab and then click Quota Entries.

In case you’re wondering, disk quotas apply to the files and folders owned by a user. (Files and folders, by the way, can be owned only by users, not groups, which is why disk quotas – with one exception – can be assigned only to users.) Here are a few other notes of interest:

Disk quotas are based on file ownership. Suppose User A creates a new file with nothing in it. User A is thus charged with 0 megabytes of disk space. However, suppose User B – who has read/write access to the file – opens the file and fills it full of pictures downloaded from the Internet. The file grows to 40 megabytes in size, all of which is charged to User A, the owner of the file.
Of course, some applications automatically change the owner of the file to the user who last modified the file. In that case the 40 megabytes of disk space would be charged to User B, but only because User B is now the owner of the file.

Disk quotas are based on the uncompressed size of a file. Compressing a file does not change the amount of disk space charged to the file owner. (Yes, we know: we were hoping to do that ourselves. But it won’t work.)

Disk quotas are sneaky: when quotas are enabled, the amount of free space reported to a user is based on their quota limit, not on the drive size. For example, suppose a user has been given 1 gigabyte of quota space on a 50-gigabyte drive. If the user immediately checks the amount of free space available on the drive, he or she will be told there is 1 gigabyte free; in other words, the amount of free space available to the user based on his or her quota. The user will never know about the other 49 gigabytes of disk space on the drive.

By default, administrators have unlimited disk space on a drive (well, limited by the amount of actual disk space); in fact, when you enable disk quotas the Administrators group is automatically added to the quota entry table and given infinite disk space. It’s probably a good idea not to change this default. If you remove the Administrators group from the quota entry table or if you limit the amount of disk space available to administrators you could seriously hinder your ability to manage the computer.

Note. Any file created by an administrator is owned by the Administrators group rather than the individual administrator. This is the one exception we talked about earlier. You can configure a disk quota entry for the Administrators group, but not for any other group. (Sure, it’s unfair. But in glancing through our list of old adages, we can’t find a listing anywhere for “Life is fair.”)

As we noted earlier, one of the major obstacles to implementing disk quotas on Windows 2000 was a lack of useful management tools. For example, suppose you wanted to generate a report showing all the disk quota entries for all the drives on a computer. In Windows 2000, there’s no easy way to do that. For one thing, don’t even consider using the Disk Quota Entries utility; there’s no way to use that tool to print information or even to save data to a text file. Likewise, while you could write a script using the Shell object, you would need to include a considerable amount of code just to identify and connect to all the drives on the machine. A bit of a hassle, and maybe not worth it. (The heck with that “No pain, no gain” stuff.)

So is this any easier in Windows 2003? Well, here’s a script that does that very thing: it returns information about all the quota entries for all the drives on a computer, and it does so with just a few lines of code:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colDiskQuotas = objWMIService.ExecQuery("Select * from Win32_DiskQuota")

For Each objQuota in colDiskQuotas
    Wscript.Echo "Disk Space Used: " & objQuota.DiskSpaceUsed
    Wscript.Echo "Limit: " & objQuota.Limit   
    Wscript.Echo "Quota Volume: " & objQuota.QuotaVolume     
    Wscript.Echo "Status: " & objQuota.Status 
    Wscript.Echo "User: " & objQuota.User     
    Wscript.Echo "Warning Limit: " & objQuota.WarningLimit
    Wscript.Echo
Next

Nice, huh? But it gets even better. Because this is WMI we can create queries that return a more targeted set of information. For example, maybe you’d like a collection of all the users who have exceeded their quota limit (that is, all quota entries with a Status property of 2). If so, just use a WQL query like this:

Set colDiskQuotas = objWMIService.ExecQuery _
    ("Select * from Win32_DiskQuota Where Status = 2")

Here’s another sample query, one that returns a list of all the users who – regardless of their quota settings – have used more than 1,000,000 KBs of disk space:

Set colDiskQuotas = objWMIService.ExecQuery _
    ("Select * from Win32_DiskQuota Where DiskSpaceUsed > 1000000")

Again, this is what makes scripting such a nice way to manage things like disk quotas. With a relatively small investment in time, you can create a management tool that works just exactly the way you need it to work.

Incidentally, when a user reaches his or her quota level, an event is recorded in the System event log on the computer where the quota violation occurred. However, no notice of any kind is issued to the user. The user will never know that he or she is approaching their quota limit until they try to save a file and are denied access because they don’t have enough disk space.

Why do we mention that? Well, you could easily write a script that returns a list of users who have exceeded their warning limit or quota limit, then have that script send an email to each user, notifying them of the situation and perhaps offering suggestions for how they can free up additional disk space. Try doing that with the disk quota management tools built into the operating system.

Top of pageTop of page

Retrieving a Single Disk Quota Entry

It should be obvious by now that the new WMI classes found in Windows Server 2003 and Windows XP make it very easy to manage disk quota settings and quota entries as a whole; that is, across all the drives on a computer. However, it’s also possible to manage individual quota entries. For example, the following script – which, we admit, is a bit scary to look at – retrieves the quota limit for user fabrikam\kenmyer on drive C of a computer:

strComputer = "."

strDrive = "C:"
strDomain = "fabrikam"
strUser = "kenmyer"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objQuota = objWMIService.Get _
    ("Win32_DiskQuota.QuotaVolume='Win32_LogicalDisk.DeviceID=" & chr(34) & strDrive & chr(34) & "'," & _
        "User='Win32_Account.Domain=" & chr(34) & strDomain & chr(34) & _
            ",Name=" & chr(34) & strUser & chr(34) & "'")

Wscript.Echo objQuota.Limit

Actually, most of the script is pretty straightforward, with one glaring exception: the WQL query used to retrieve the quota entry. As you probably noticed, the query is filled with ampersands, single quotes, double quotes, and all sorts of punctuation. It’s not a pretty sight (even though beauty is in the eye of the beholder), but that’s just the way that individual quota entries are tracked by WMI. For example, here’s what quota entry instances look like in Wbemtest:

Disk Quotas


To see as full-size version of this graphic, please click here.

In other words, for better or worse (and, yes, it probably is worse), the key property for a disk quota entry is an amalgamation of the Win32_DiskQuota, Win32_LogicalDisk, and Win32_Account classes. And there’s just no way of getting around that.

But don’t worry about it. The preceding script has been designed to make it easy for you to modify and use. Part of the reason the WQL query is so cryptic-looking is that we have used variables to represent the drive, user, and user domain. To use the script, you only have to assign the appropriate values to the three variables representing these properties:

strDrive = "C:"
strDomain = "fabrikam"
strUser = "kenmyer"
Top of pageTop of page

Modifying a Disk Quota Entry

Both the Limit and WarningLimit properties are read/write: that means you can use a script to modify the values of either of these properties. For example, here’s a script that modifies the drive C Limit value for the user fabrikam\kenmyer:

strComputer = "."

strDrive = "C:"
strDomain = "fabrikam"
strUser = "kenmyer"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objQuota = objWMIService.Get _
    ("Win32_DiskQuota.QuotaVolume='Win32_LogicalDisk.DeviceID=" & chr(34) & strDrive & chr(34) & "'," & _
        "User='Win32_Account.Domain=" & chr(34) & strDomain & chr(34) & _
            ",Name=" & chr(34) & strUser & chr(34) & "'")

objQuota.Limit = 11111111
objQuota.Put_

As usual, make sure to call the Put_ method after assigning new values. If you don’t call Put_ no error will be generated; however, no changes will be made to the quota entry, either.

Of course, you could also change the Limit for all the users on the drive. Here’s a script that does that very thing:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colDiskQuotas = objWMIService.ExecQuery _
    ("Select * from Win32_DiskQuota Where QuotaVolume = 'C:\\'")

For Each objQuota in colDiskQuotas
    objQuota.Limit = 11111111
    objQuota.Put_
Next
Top of pageTop of page

Deleting a Disk Quota Entry

Sometimes disk quota management involves nothing more than solving simple mathematical equations. For example, suppose you have 100 users and 100 gigabytes of available disk space. If you wanted to, you could simply divide the amount of available disk space by the number of users, and thus allocate everyone 1 gigabyte of disk space.

Of course, this kind of calculation is useful only if the numbers involved are valid. If you actually have 50 users, you might be shortchanging everyone; if you have 200 users you are allocating more disk space than actually exists. Because of that, you need to keep a close eye on both the amount of available disk space and on the number of quota entries on the drive. And to do the latter you need to routinely delete invalid quota entries; for example, quota entries for users who are no longer with your organization or who no longer have access to the drive. (You need to do this manually because quota entries are not automatically deleted any time a user account is deleted.) Fortunately, this is another task that can be carried out using a script.

To delete a quota entry all you have to do is connect to that entry and then call the Delete_ method; you don’t even have to call Put_ to write the changes to the operating system. Instead, the moment you call Delete_ the quota entry is deleted.

Here’s a script that deletes the quota entry on drive C for the user fabrikam\kenmyer:

strComputer = "."

strDrive = "C:"
strDomain = "fabrikam"
strUser = "kenmyer"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objQuota = objWMIService.Get _
    ("Win32_DiskQuota.QuotaVolume='Win32_LogicalDisk.DeviceID=" & chr(34) & strDrive & chr(34) & "'," & _
        "User='Win32_Account.Domain=" & chr(34) & strDomain & chr(34) & _
            ",Name=" & chr(34) & strUser & chr(34) & "'")

objQuota.Delete_

Keep in mind that deleting a quota entry does not affect the user’s ability to store files on the drive. Suppose you delete the quota entry for Ken Myer, and a few moments later Ken attempts to store a file to that drive. Will Ken be denied access to the drive? No; he’ll be able to save the file, and a brand-new quota entry will be created for him. If you want to prevent a user from saving files to a drive you’ll have to use NTFS permissions rather than disk quotas.

Also bear in mind that you cannot delete a quota entry for any user who still owns files on the drive. Instead, you’ll need to delete those files, move those files, or take ownership of those files before the quota entry can be deleted.

Top of pageTop of page

Creating a New Disk Quota Entry

As we noted earlier, you do not have to create a new quota entry for each new user who might save a file to a drive. That’s the reason for the default quota and default warning limits. When a user saves a file, the operating system looks up both the user and his or her quota settings. If the user does not have an existing quota entry the operating system will automatically create a new quota entry based on the default settings. This makes disk quota administration much easier.

However, there might be times when you do not want those default settings applied to a user. For example, you might want to give graphic artists more disk space than you allot to receptionists. In a case like that, you could wait until the graphic artist has saved a file to the disk – thus generating a new quota entry based on the default settings – and then use a script to modify those settings. Alternatively, you might be able to save some time and trouble by using a script to create a quota entry in advance, giving that user additional disk space right from the start.

Again, be forewarned: the script to create a new quota entry will look a little crazy. However, the sample script has also been designed to make it easy for you to use: all you have to do is assign the appropriate values for the drive, user name, and domain, and you’ll be able to use this script as-is.

strComputer = "."

strDrive = "C:"
strDomain = "fabrikam"
strUser = "kenmyer"


Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objAccount = objWMIService.Get _
    ("Win32_Account.Domain='" & strDomain & "',Name='" & strUser & "'")
Set objDisk = objWMIService.Get _
    ("Win32_LogicalDisk.DeviceID='" & strDrive & "'")
Set objQuota = objWMIService.Get _
    ("Win32_DiskQuota").SpawnInstance_

objQuota.QuotaVolume = objDisk.Path_.RelPath
objQuota.User = objAccount.Path_.RelPath
objQuota.Limit = 10485760
objQuota.WarningLimit = 8388608
objQuota.Put_

Yes, we know: this is a slightly more complicated script than we’ve been dealing with. That’s due in large part to the fact that we need to create object references that represent both the user account (objAccount) and the drive where the quota entry will be created (objDisk). That’s what we do in these two lines of code:

Set objAccount = objWMIService.Get _
    ("Win32_Account.Domain='" & strDomain & "',Name='" & strUser & "'")
Set objDisk = objWMIService.Get _
    ("Win32_LogicalDisk.DeviceID='" & strDrive & "'")

As soon as we have these two object references we can then use the SpawnInstance_ method to create a blank quota entry. After that we simply specify values for the QuotaVolume, User, Limit, and WarningLimit properties. The tricky part is that we use the Path_.RelPath property of our two object references when setting the values for the QuotaVolume and User properties. The Path_.RelPath values for the disk drive and for the user look something like this:

Win32_LogicalDisk.DeviceID="C:"
Win32_UserAccount.Domain="fabrikam",Name="kenmyer"

We use the Path_.RelPath property as a matter of convenience: it’s a little cleaner and neater than using another of those crazy concatenated strings with all the ampersands, single quotes, and double quotes.

And, of course, we then use the Put_ method to actually create the quota entry.

Top of pageTop of page

For More Information …

You know what they say: all good things must come to an end, and that applies to this article as well. If you have questions or comments about using scripts to manage disk quotas, please send them to scripter@microsoft.com. In the meantime, we’re out to disprove that old adage about not having your cake and eating it, too. Wish us luck.


Top of pageTop of page