This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
    
          
     
 
		 
          
This script will scan a local or remote disk for MP3 files, uses a Regex to extract the user from the Path, and than groups them by user to generate a Summary Report like this :
Report for MP3 files on .
Drive: G:
Number of MP3 files
688
Total Size (MB)
4745,23
Max size (MB)
142,51
MP3 Per User
User                                    Count                                            MB
----                                    -----                                            --
Usr001                                    141                                        998,52
Usr002                                    547                                       3746,62
the first part of the script (getting the list, and making the summary I did real quick)
an interesting is the convering to MB part, I'm using the .... 
(oops, how are they called) that MSH has defined for Binary Numbers, K = KiloByte etc., you can use them as a number.
MSH>1k
1024
MSH>1m
1048576
MSH>1g
1073741824
very handy.
but then I wanted to sort them by user, the username was in the path.
it was pretty easy to get them out using a regex but how to select on that.
Select-object lets you use expressions in the statement, but I got stuck here.
$mp | select { 
$_.name;($re.Match($_)).groups[1].value;[math]::round(($_.filesize / 1m) , 2 
)}
MSH C:\WINDOWS\system32> $mp | select { 
$_.name;($re.Match($_)).groups[1].value;[math]::round(($_.filesize / 1m) , 2 
)}
$_.name;($re.Match($_)).groups[1].value;[math]::round(($_.filesize / 1m) , 2 )
------------------------------------------------------------------------------
{g:\userdata\home\User1\data\dikkie dik.mp3, User1, 2.06}my properties where melted together and the values not realy usable anymore.
(I managed to fix the "melting" by splitting the scriptblock, but I already got a better solution : )
but again the NG quickly provided an answer, Keith Hill showed me the use of the "Expression=" - statement/structure to format the Calculated Properties.
so Now I can easy format and name the Calculated properties in Monad so the scriptproperties are much more usable after that.
I make a list with al the userdata needed for the Usage per user summary and place it in the $UserList with this method,
After that I group the $userfiles data by username, and then for every user I use the Group property (Created by the Group statement ) to create the Count and MB value again with the same method.  
the final script looks like this :
$computer = "." 
$drive = "G:" 
$Extension = "MP3" 
# RegExfilter for extracting Username from Path 
$re = new regex("\\(usr.*?)\\") 
# Seach the Files 
$files = get-wmiobject -computer $computer cim_datafile -filter "Drive = '$drive' AND Extension = '$Extension'"  
# Summary  
"`nReport for $Extension files on $Computer`nDrive: $drive " 
"`nNumber of $Extension files" 
($files | Measure-Object ).count 
"`nTotal Size (MB)" 
[math]::round(($files | Measure-Object -property filesize -sum).sum / 1m , 2 ) 
"`nMax size (MB)" 
[math]::round(($files | Measure-Object -property filesize -max).max / 1m , 2 )  
# make list of MP3 with UserName extracted from path by Regex  
$Userfiles = $files | select @{expression={$_.filename}; name="Filename"}, 
             @{expression={[math]::round(($_.filesize / 1m) , 2 )}; name="RoundedSize"}, 
             @{expression={($re.Match($_)).groups[1].value}; name="UserName"} 
# group them per user  
"`n$Extension Per User" 
$Userfiles | group Username | select @{expression={$_.Name};Name="User"}, 
                               @{expression={($_.group | measure-object).count};Name="Count"}, 
                               @{expression={($_.group | measure-object -property RoundedSize -sum).sum};Name="MB"} 
You can see how powerfull the usage of Select-Object, Measure-Object and Group-Object are Combined with the "Expression=" structure to name the generated properties. 
enjoy,
Greetings /\/\o\/\/
Tags : Monad msh
 
          
 