Objects are the answer

As I usually do, I was helping out in the forums at ScriptingAnswers.com. A member had several variables that he wanted to show as a group. As is almost always the case, the answer in PowerShell is object. I talk about this all the time in classes, conferences and online chitchat. Let me offer up a script that demonstrates how objects are the answer.

My business need was to report on files found in a given folder or path. I wanted to get a breakdown of file types showing me the number of each type, their total size and their percentage of all files by count and size.

The script takes a path as a parameter but defaults to your documents folder. Get-Childitem retrieves all files. I’m setting the –errorAction parameter to silentlycontinue to avoid the annoying error messages you get when trying to read protected system folders. The collection of files are then grouped by extension. Each group is then analyzed.

At this point I have a number of pieces of information to display. In a VBScript I would have most like written some message to the screen for each file type. Technically I could do the same thing here but why? Using New-Object I create a new object with properties for all my data. The objects written to the pipeline can be manipulated just like any other object. Let me check my C: drive.

PS C:\> $c=c:\scripts\filestats.ps1 “c:\”

Hmmm…how many different file types are on my computer?

PS C:\> $c.count

Which file types are taking the most space?

PS C:\> $c | sort totalsizeMB -desc | select -first 10 -property * -ExcludeProperty Files | format-table -auto

TotalSizeMB PercentTotal Total Extension PercentSize

———– ———— —– ——— ———–

115692.94 0.0637 %        90 .vmdk     57.8050 %

19559.24 0.0219 %        31 .iso      9.7726 %
14375.12 0.0014 %         2 .vdi      7.1824 %

12440.64 14.8798 %    21018 .dll      6.2159 %
6972.15 2.4778 %      3500 .exe      3.4836 %
6190.48 0.0064 %         9 .wim      3.0930 %

2884.98 0.1373 %       194 .CAB      1.4415 %

2672.56 0.0021 %         3 .m4v      1.3353 %

1424.91 0.0042 %         6 .sav      0.7119 %
1202.12 0.0092 %        13 .mp4      0.6006 %

My custom object even includes a property, files, which itself is a collection of objects. For example, what are the .vdi files?

PS C:\> $c | where {$_.extension -eq “.vdi”} | select -ExpandProperty Files

Directory: C:\VirtualBox\Exchange

Mode                LastWriteTime     Length Name
—-                    ————-     —— —-
-a—         6/29/2010   1:58 PM 1507336243 ExchangeHDD.vdi

Directory: C:\VirtualBox\R2

Mode                LastWriteTime     Length Name
—-                    ————-     —— —-
-a—         6/21/2010   9:34 PM      41472 ExchangeStore.vdi

The bottom line is that having objects offers tremendous possibilities. So the next time you’re working on a PowerShell problem, know that objects are the answer.

My script was intended as a demonstration but if you’d like a copy

New File Shortcut

I’ve been looking at the File Server Resource Manager (FSRM) feature in Windows Server 2008 R2 a lot lately. One very nice part of FSRM is the ability to schedule typical file management tasks. One of the examples from the Microsoft storage team is to create a custom task to move old files to another server and leave a shortcut in place. The files are moved to a more appropriate server and the users can still find their files with practically no fuss. However, this requires Windows Server 2008 R2. If you aren’t there yet, you can use my PowerShell function, New-FileShortCut in your archiving process. Continue reading “New File Shortcut”

Get Disk Quota

During a recent class I was teaching, a student asked about a way to get disk quota reports from Windows file servers.  I knew there was a WMI class, Win32_DiskQuota, and had some old VBScript files. However, they were pretty basic and not as robust as I would have liked. So I worked up PowerShell v2 function called Get-DiskQuota. Continue reading “Get Disk Quota”

All Hail Dir UseALot!

Some of you know my relationship with the a command prompt goes back a long, long way. Naturally I became very adept at using the DIR command, fully taking advantage of its switches to tease out hidden information or to quickly get just the information I wanted. When PowerShell first came out, I made the transition to the DIR alias without too much difficulty. Although I still found myself wanting to run a command like DIR /ad  (ie list only directories). Yes, you can achieve the same results with the PowerShell cmdlets, but that takes too much typing, especially for something I might want to use on a regular basis. I finally got around to writing a DIR function for PowerShell that better emulates my beloved DIR command from the CMD shell. Continue reading “All Hail Dir UseALot!”

Is That Folder Empty?

In keeping with my recent trend of offering solutions based on PowerShell v2.0, here’s a function I’ve revised to test if a folder is empty. I can’t recall where I used the original function or if I ever did. But I came across it recently and decided to give it a facelift. Manually determining if a folder is empty is pretty easy. If it doesn’t have any files, I consider it empty. What I originally wanted was a way to find all empty folders, say from a given root folder. Once I’ve identified the folders I can do something with them like whack ’em. Here’s my PowerShell v2 function.

Continue reading “Is That Folder Empty?”