Mr. Roboto

Get Answers to Your Storage Questions

Need help managing your file server storage? Check out Mr. Roboto's PowerShell functions.

You probably spend more time than you'd like keeping an eye on your file servers' folder utilization. By using Windows PowerShell and a few of Mr. Roboto's PowerShell functions, you can get a handle on your file server storage in no time.

These PowerShell functions take a folder path as a runtime parameter. You can specify either a traditional drive letter path or a UNC. You'll find they run a little faster if you have PowerShell installed on your file server. However, there's no reason you can't use them locally on your desktop and query either a UNC or mapped drive.

The functions I've written are in two PowerShell scripts. One function-Get-ExtensionReport-examines a specified folder and returns the total number of files and total size in bytes for each file extension found in the folder and subfolders. The other function-Get-FolderReport-retrieves the same information (number of files and size) for every folder and subfolder.

The folder report only returns information based on files in the root of each folder. It doesn't add up the sizes and counts of any nested folders. Both functions will search recursively by default, but you can disable that by passing $False as the second parameter. The functions also use the Write-Progress cmdlet to display a progress bar. This is especially useful when analyzing large folder hierarchies.

The scripts demonstrate the functions. You could copy and paste each function into your PowerShell session, but they'd be gone as soon as the session ended.

Another option is to "dot-source" the script. At a PowerShell prompt, type a period followed by the script's path:

PS C:\> . 
  c:\scripts\powershell\get-
  folderreport.ps1

This loads the function into the main PowerShell session. From this point on, you can call the function directly by its name. You'll still be prompted for a folder, unless you comment out the end of the script before it's dot sourced.

Your final option is to copy and paste each function into your profile. This will ensure that you always have the function available when PowerShell starts.

Surfing the Pipeline
The functions participate in the PowerShell pipeline and create custom objects with size and count properties. Because they work this way, you can run PowerShell expressions like these (all expressions are single-line commands):

Display a table of file extensions sorted by size in descending order:

get-extensionreport c:\files | sort 
  size -desc | format-table -auto

Display a table of the top 10 folders sorted by number of files:

get-folderreport c:\files | 
  sort count -desc | select -first 10 |
   format-table -auto

Display a list of extensions and the total size for each extension:

get-extensionreport c:\files | 
  where {$_.size -ge 10mb} | sort size -desc

Use the Measure-Object cmdlet to get a quick statistical report:

get-folderreport c:\files | measure-
  object size -sum -max -min -average

You can also create a nice graphic if you have PowerGadgets installed:

get-extensionreport c:\files | sort 
  size -desc | select -first 10 | out-
  chart -values Size -label extension 
  -title "File Type Report C:\Files"

Because PowerShell is object based and my functions emit objects, we have many opportunities to leverage this information. Want to build a simple HTML report for management? Try this:

get-extensionreport c:\files | sort 
  size -desc | convertto-html -title 
  "Extension Usage for C:\Scripts" | 
  out-file c:\reports\filereport.html

If you're using Windows Vista and querying your docs folder, you may get some "Access denied" errors. This is because of Vista's junction points. The functions will continue and you should be able to ignore the messages, and the final numbers should still be pretty accurate.

If you've been looking for a reason to start working with PowerShell this should get you on the right path. If you've been using PowerShell for a while, you'll come up with plenty of other uses for these functions.

Roboto on Demand

Download Mr. Roboto's PowerShell functions at: www.jdhitsolutions.com/scripts.

What Windows admin task would you like Mr. Roboto to automate next? Send your suggestions to jhicks@redmondmag.com.

About the Author

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for MPCMag.com and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at PowerShell.org. Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog http://jdhitsolutions.com/blog, on Twitter at twitter.com/jeffhicks and on Google Plus (http:/gplus.to/JeffHicks)

comments powered by Disqus

Reader Comments:

Wed, Feb 18, 2009 Anonymous Anonymous

get this box out of the way please!

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.