Mr. Roboto
Get Answers to Your Storage Questions
Need help managing your file server storage? Check out Mr. Roboto's PowerShell functions.
- By Jeffery Hicks
- 02/01/2008
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.
About the Author
Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.