Make a tedious task easier, without a lot of scripting
| Downloads |
|---|
| 143789.zip |
Managing file servers can be a tedious and thankless task for many IT pros. But it doesn't need to be that way. By incorporating Windows PowerShell, you can easily get a handle on shared file resources -- and maybe even have a little fun. And none of it really requires any scripting. Everything I want to show you should take no more than a few interactive commands. Of course, if you find yourself running these commands often, then turning them into a script or function is the next logical step.
The beauty of PowerShell is that you can take the results of the commands that I'm going to demonstrate and do whatever you want. Need to save the results to a comma-separated value (CSV) file? Pipe them to Export-CSV. Need an HTML report? Use ConvertTo-HTML. And everything I'm going to show you scales; if you can use a command for one computer, you can use it for 10, 100, or 1,000 systems.
First, let me show you what you can do to manage what you have in your file shares today. Then we'll look at provisioning file sharing.
Get File Shares
Let's begin by identifying what's being shared. This task is easy: Simply query the Win32_Share class to use Windows Management Instrumentation (WMI). You don't even need to be logged on to the file server. You can run this command from the comfort of your cubicle:
Get-WmiObject -class -Win32_Share -computername MyFile Now, when you run this command, you'll get all shares, including printers (if any). Because we're talking about file shares, let's limit the query. All Win32_Share instances have a Type property, as Table 1 shows. Thus, to limit the search, we can add a filter to our original command:
"Type=0"
This approach gets rid of the administrative shares. You can see an example in Figure 1.

Figure 1: Listing Non-Administrative Shares with WMI
But if you're looking for other hidden shares -- that is, those that end in a dollar sign ($) -- all you need is a slight tweak to the filter:
"Type=0 AND name like '%$'"
In WMI, the percent character (%) is used as a wildcard. Returning all shares except those that are hidden is a little trickier. You'll need to use a compound comparison using a wildcard:
"type=0 AND name like '%[^$]'"
This command returns all Win32_Share objects that have a Type property of 0 and a name that doesn't end in $.
Get Folder Size
A typical task that's probably on your plate is creating reports about how much disk space a folder is consuming. The quick approach is to simply use Get-ChildItem, or its alias dir, and pipe the results to Measure-Object:
Measure-Object -Property length -Sum -Minimum -Maximum
You'll end up with a measurement object that shows the total number of objects, the total size in bytes, and the smallest and largest file sizes. In the previous command, I've filtered out folders. PowerShell 3.0 has better ways of doing this, but the command that I've used works in both PowerShell 2.0 and 3.0. This is the type of command that is best run locally (a great reason to use PowerShell remoting). The code in Listing 1 combines this command with our WMI technique to get a size report for top-level folders. You can format or process $results any way you like. How about an easy-to-read table? Just use this command:
NumberFiles -autosize
Figure 2 illustrates the output that you can expect.
It doesn't take much more effort to build a comprehensive usage report for all shares on a file server. I'll save you the time: Take a look at Listing 2. Again, I can slice and dice $results any way I need. Figure 3 shows one approach.

Figure 3: Usage Report for File Server Shares
Get Files by Owner
A variation on this theme is to find file usage by owner. If you use quotas, you most likely already have reporting in place. Otherwise, all you need to do is retrieve the file ACL, which includes the owner, and aggregate the results. I find that the best approach is to add the owner as a custom property:
select name, @{Name="Owner";Expression=
{(Get-ACL $_.fullname).Owner}}, length
We can group this output by the new owner property, and then process the new object:
{($_.Group | Measure-Object -Property Length -sum).Sum}}






