PowerShell for File Management (Part 3)

If you would like to read the other parts in this article series please go to:

So far in this article series, I have shown you various techniques for interacting with the file system. What you might not realize however, is that you can also use PowerShell to perform tasks that would otherwise require you to use the File Server Resource Manager. For example, you can set file screens and quotas through PowerShell.

With that said, let’s start out by taking a look at how you might set a quota on a folder by using PowerShell. For this example, I am going to create a folder named C:\Demo, and then set a quota of 2Gb. To get started, let’s go ahead and create the folder by using these commands:

C:
CD\
MD Demo
Get-ChildItem

The first of these commands switches to the C: drive. The second command navigates to the root directory, and the third command creates a folder named Demo. The last command verifies the folder’s existence, as shown in the figure below:

Image
I have created a folder named Demo.

Now that we have created a folder, we need to install the File Server Resource Manager. Otherwise, none of its related PowerShell commands will work. You can install the File System Resource Manager by entering the following command:

Install-WindowsFeature -Name FS-Resource-Manager -IncludeManagementTools

You can see how this works below:

Image
You must install the File Server Resource Manager.

You should now be able to use File Server Resource Manager related commands, but some older versions of PowerShell may require you to manually import the File System Resource Manager module first. You can do this by typing:

Import-Module FileServerResourceManager

Now that all of the necessary pieces have been put into place, let’s apply a quota to the folder. The cmdlet used for doing so is New-FsrmQuota. When using this command, there are three parameters that we need to provide -Path, -Description, and -Size. The path is of course, the name of the folder to which the quota will be applied. The description is a text description of the quota, and the size is the quota itself. Here is an example of the command in use:

New-FsrmQuota -Path “C:\Demo” -Description “The C:\Demo folder will have a quota of 2 GB” -Size 2.0GB

You can see this command in action below:

Image
We have used the New-FsrmQuota cmdlet to apply a 2Gb quota to the C:\Demo folder.

The command that I just showed you created what is known as a hard quota. In other words, a quota of 2Gb has been enforced on C:\Demo. There are no warnings, there is no grace period, and there are no exceptions. However, we can tone things down a little bit to make the quota a bit less draconian.

Before we do, let’s take a look at the quota, and then let’s remove it. You can view the quota by typing this command:

Get-Quota C:\Demo

As an alternative, you could enter the Get-Quota cmdlet without a path. Doing so would display every quota that exists on the server.

We can remove the quote by using the Remove-FsrmQuota cmdlet, followed by the path to which the quota has been applied. In this case, the command would look like this:

Remove-FsrmQuota -Path “C:\Demo”

You can see what this looks like in the figure below:

Image
You can use PowerShell to display and remove a quota.

In the figure above, the error resulted from trying to display a quota that no longer exists.

So what if we wanted to make the quota a little less firm? One way of doing so would be to append the -SoftLimit parameter to the end of the New-FsrmQuota cmdlet. Doing so tells PowerShell that quota violations should be reported, but not enforced.

Another thing that we can do is to create a warning when the folder is approaching the quota limit. This technique is a bit more complicated, but it can be achieved using three lines of code. Here is the code as provided by Microsoft.

$Action = New-FsrmAction -Type Command -Command "c:\windows\system32\cmd.exe" -CommandParameters "echo  >> c:\log.txt" -ShouldLogError
$Threshold = New-FsrmQuotaThreshold -Percentage 90 -Action $action
New-FsrmQuota -Path "C:\Shares" -Size 128MB -Threshold $Threshold -Softlimit

There is a lot going on here, but let’s break this down into its basic actions. Our goal is to log a warning message when the folder is approaching its quota limit. As such, there are two pieces of information that will need to be included in the quota. The first of these pieces of information is the threshold value. This is a percentage value that indicates when a warning should be given. The code above uses 90 as the threshold value, meaning that it will generate a warning when 90% of the quota has been reached.

The other thing that we have to provide is an action. We are setting 90% as our threshold, but what action is taken when the threshold is reached? That’s what we have to define.

So with that in mind, take a look at the code above. The first line of code defines a variable named $Action. This variable contains the action that will be performed when the threshold is reached. In this case, the action involves running a command. The command that is run is C:\windows\system32\cmd.exe. This causes Windows to open a Command Prompt window. Next, there are some command parameters that have been defined. In this case, those parameters are “echo >> c:\log.txt”. This causes the folder’s path to be written to a log file named C:\log.txt. Echo is a Command Prompt command for displaying text, and >> is a redirection command, meaning that the text, in this case the source file path, should be written to a file rather than being displayed on screen.

The next line of code starts out by defining a variable named $Threshold. This variable sets the FsrmQuotaThreshold. The threshold value in this case will be 90%. Notice that the $Action variable is being referenced through the $Threshold variable and the -Action parameter.

The last line of code actually creates the quota. This line works very similarly to what you saw earlier, except that Microsoft has omitted the description text, and added the -Threshold parameter and the -Softlimit parameter. The -Threshold parameter references the $Threshold variable, which sets up the threshold value and the threshold action. As previously mentioned, the -SoftLimit parameter tells PowerShell that the quota limit should not be enforced.

Final thought

As you can see, you can use Windows PowerShell to create and remove FSRM quotas. These quotas can be basic, or they can be more advanced, establishing threshold values and actions.

Incidentally, if you create a quota and then later decide that you want to change it, you do not have to remove and then recreate the quota as I have done in this article. I only deleted and reconstructed the quota because I wanted to demonstrate the process for doing so. As a shortcut, you can modify an existing quota by using the Set-FsrmQuota cmdlet.

In the next article in this series, I plan to discuss FSRM file screens.

If you would like to read the other parts in this article series please go to:

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top