The Surly Admin

Father, husband, IT Pro, cancer survivor

Get File Counts and Size for All Extensions

Inspired from this script, I decided to expand on it and have Powershell get all of the extensions and report on everything.  I also decided to pretty up the report using some HTML.  Read on for this quick hit!

We’ll need slightly different user defined variables here then in the original script.  Still need the $Path variable to define where we want to look and I’ll keep the common dialog box because it’s cool!  Since we’re doing an HTML report we’ll need to define a place to save the final HTML file.

Param (
[string]$Path = ($((New-Object -ComObject "Shell.Application").BrowseForFolder(0,"Select a folder:",0)).Self.Path),
[string]$OutputPath = "c:\utils"
)

Next few lines are some here-strings where I define my static HTML, I won’t post them here on the blog for a couple of reasons.  It’s a lot of text that isn’t really Powershell, and second whenever I use the schedule post function on WordPress it converts all of my double quotes, less thens and greater thens to the HTML equivalent.  I then have to go back into the post and re-edit them and then it’s OK.  Since HTML is chalk full of those things it’s always painful to post them!

$AllFiles = Get-ChildItem $Path -Include * -Recurse | Where { $_.PSisContainer -eq $false }
$AllSum = ($AllFiles | Measure-Object Length -Sum).Sum

Here I’ve slightly modified the Get-ChildItem cmdlet to get all files, but ignore any of the folder objects.  I then take $AllFiles and SUM the length property to get a grand total in space which I need to make my percentage calculations later.

$Extensions = $AllFiles | Select Extension -Unique | Sort Extension
ForEach ($Ext in $Extensions)

Here I determine all of the unique extension types in our files collection, and begin looping through them.  Instead of storing this in $Extensions I could have simply piped the command straight into the ForEach object and used $_ instead of using the ForEach statement but this was one of those times where I was more interested in the code be readable then taking advantage of Powershell coolness.  Less efficient?  Maybe.  But a lot easier to read.


$Files = $AllFiles | Where { $_.Extension -eq $Ext.Extension }
$FilesSum = ($Files | Measure-Object Length -Sum).Sum
$Percent = "{0:N0}" -f (($FilesSum / $AllSum) * 100)

Now we search the files collection for our first extension, sum that number up and calculate the percentage.  This is passed into a table line–sorry for not posting it but like I said WordPress eats this stuff for lunch!  But the “gist” of it is I use the percentage that was calculated above to determine how big a <div> is within the table.  This <div> has a green background color and will essentially mimic a graph bar and show how much of the total this particular extension takes of the entire path.

That’s really about it.  I combine the different HTML parts into a variable and export it to the $OutputPath which can be a web server, Sharepoint location, whatever.  You can even add some code in there to email it if you want.

Enjoy!

Find the full source code here.

Advertisements

November 26, 2012 - Posted by | PowerShell | ,

2 Comments »

  1. Great post very useful
    I was wondering if you could add something some extra functionality to it, literate through each directory from the $Path and create a new table giving FullName, FileSize, Name, CreatedTime, I have been messing with it but have only been successful using the | ConvertTo-HTML which adds extra html code

    Comment by condrovic | May 21, 2015 | Reply

  2. Great Script, how hard would it be for create new tables for each of the Directories from $Path? I have been attempting to modify it to output tables with the following info for Each directory FullName, Name, Size, CreateTime sorted by extension. I am able to do this using the ConvertTo-Html but as you know it adds extra html code.

    Thanks

    Comment by condrovic | May 21, 2015 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: