#requires -version 2.0 # ----------------------------------------------------------------------------- # Script: Get-Extension.ps1 # Version: 2.0 # Author: Jeffery Hicks # http://jdhitsolutions.com/blog # http://twitter.com/JeffHicks # http://www.ScriptingGeek.com # Date: 6/23/2011 # Keywords: File, Measure-Object, DIR, Group-Object # Comments: # # "Those who forget to script are doomed to repeat their work." # # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** # ----------------------------------------------------------------------------- Function Get-Extension { <# .SYNOPSIS Get file statistics based on extension .DESCRIPTION This command will search a given folder and analyze file size by extension. The function will write a custom object to the pipeline for each file type. Average : 53830685.8333333 Largest : 252654900 Path : c:\work TotalSize : 645968230 Extension : avi Count : 12 Smallest : 3229258 You can search recursively as well as filter using the -Include or -Exclude parameters. These parameters require -Recurse. Recursion will automatically be used if you specify -Include and/or -Exclude. .PARAMETER Path The folder path to analyze. The default is the $ENV:TEMP folder. Do NOT include any file types in the path. .PARAMETER Include Retrieves only the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as "*.txt". Wildcards are permitted. .PARAMETER Exclude Omits the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or patter n, such as "*.txt". Wildcards are permitted. .PARAMETER Recurse Search recursively from the specified path. This is required and automatically enabled if you use -Include or -Exclude. .PARAMETER Force Allows the function to get items that cannot otherwise not be accessed by the user, such as hidden or system files. The Force parameter cannot override security restrictions. .PARAMETER AsJob Run the command in a background job .EXAMPLE PS C:\> get-extension c:\work | sort TotalSize -descending | Select -first 3 Analyzing c:\work Average : 53830685.8333333 Largest : 252654900 Path : c:\work TotalSize : 645968230 Extension : avi Count : 12 Smallest : 3229258 Average : 10045236 Largest : 133082663 Path : c:\work TotalSize : 140633304 Extension : zip Count : 14 Smallest : 67793 Average : 21041152 Largest : 21041152 Path : c:\work TotalSize : 42082304 Extension : evt Count : 2 Smallest : 21041152 Get the top 3 file types in C:\Work .EXAMPLE PS C:\> Get-Extension c:\scripts -include *.ps1,*.vbs,*.bat -recurse Analyzing c:\scripts Average : 533.5 Largest : 1087 Path : c:\scripts TotalSize : 3201 Extension : bat Count : 6 Smallest : 127 Average : 3115.2527693857 Largest : 93134 Path : c:\scripts TotalSize : 3093446 Extension : ps1 Count : 993 Smallest : 0 Average : 9286.16666666667 Largest : 41665 Path : c:\scripts TotalSize : 55717 Extension : vbs Count : 6 Smallest : 416 File usage in the script directory, searched recursively. .EXAMPLE PS C:\> Get-Extension $env:userprofile\documents -include *.doc*,*.ppt*,*.xls* | format-table -AutoSize Analyzing C:\Users\Jeff\documents recursively include *.doc*,*.ppt*,*.xls* Average Largest Path TotalSize Extension Count Smallest ------- ------- ---- --------- --------- ----- -------- 185289.386666667 1196032 C:\Users\Jeff\documents 13896704 doc 75 29696 137551.418994413 3303878 C:\Users\Jeff\documents 24621704 docx 179 12579 2409943.33333333 5831680 C:\Users\Jeff\documents 7229830 ppt 3 436614 813461.2875 6789940 C:\Users\Jeff\documents 65076903 pptx 80 47558 56768 138240 C:\Users\Jeff\documents 454144 xls 8 24064 16834.8815789474 42335 C:\Users\Jeff\documents 1279451 xlsx 76 8790 Find Microsoft Office file usage in the user's Documents folder. .EXAMPLE PS C:\> Get-Extension $env:userprofile\documents -include *.doc*,*.ppt*,*.xls* -AsJob Id Name State HasMoreData Location Command -- ---- ----- ----------- -------- ------- 31 Job31 Running True localhost Param([string]$cmd)... Get the user's Microsoft Office files but using a background job. .NOTES NAME : Get-Extension VERSION : 2.0 LAST UPDATED: 6/23/2011 AUTHOR : Jeffery Hicks .LINK http://jdhitsolutions.com/blog/2011/06/get-file-utilization-by-extension .LINK Get-ChildItem Measure-Object Group-Object .INPUTS None .OUTPUTS Custom object #> [cmdletbinding(DefaultParameterSetName="*")] Param ( [Parameter(Position=0)] [validateNotNullorEmpty()] [string]$Path=$env:temp, [Parameter(ParameterSetName="Filter")] [string[]]$Include, [Parameter(ParameterSetName="Filter")] [string[]]$Exclude, [Parameter()] [switch]$Recurse, [switch]$Force, [switch]$AsJob ) Write-Verbose "Starting $($myinvocation.mycommand)" #Automatically turn on recursion if -Include or -Exclude is used if ($Include -OR $Exclude) { $Recurse=$True } #test and see if path is valid if (Test-Path -Path $path) { #Verify we are using a FileSystem path if ((Get-Item -Path $path).PSProvider.Name -ne "FileSystem") { Write-Warning "$($path.ToUpper()) is not a valid file system path." Return } #define a variable for message to be displayed when the search begins $msg="Analyzing $path" #build a command string based on parameters $cmd="Get-ChildItem -Path $path" if ($recurse) { $msg+=" recursively" $cmd+=" -recurse" } if ($include) { $ofs="," $msg+=" include $($Include -as [string])" $cmd+=" -include $($Include -as [string])" } if ($exclude) { $ofs="," $msg+=" exclude $($Exclude -as [string])" $cmd+=" -exclude $($Exclude -as [string])" } if ($force) { $cmd+=" -force" } #wrap the core commands into a scriptblock so it can be executed directly #or used with Start-Job $sb={Param([string]$cmd,[string]$Path) Write-Host $msg -ForegroundColor Green #get all files but exclude folders Write-Verbose "Executing: $cmd" $files= Invoke-Expression $cmd | where {-NOT $_.PSIsContainer} #put files into groups based on extension $group=$files | Group-Object -Property Extension Write-Verbose "Found $($group.count) file extensions" foreach ($extension in ($group | Sort Name)) { #calculate statistics for each group Write-Verbose "Measuring $($extension.name)" $stats=$extension.group | measure-object -Average -Sum -Minimum -Maximum -Property length #trim off the period from the extension if it exists if ($extension.name -match "\.") { $ext=$extension.name.Substring(1) } else { $ext=$extension.name } #write a custom object to the pipeline New-Object -TypeName PSObject -Property @{ Count=$stats.count #trim off the period Extension=$ext TotalSize=$stats.sum Largest=$stats.maximum Smallest=$stats.minimum Average=$stats.average Path=$Path } } #foreach }#$sb if ($AsJob) { Write-Verbose "Creating background job" Start-Job -ScriptBlock $sb -ArgumentList $cmd,$path } else { Invoke-Command -ScriptBlock $sb -ArgumentList $cmd,$path } } #if else { Write-Warning "Failed to find $path" } Write-Verbose "Ending $($myinvocation.mycommand)" } #end function #optionally, add an alias #Set-Alias -name gex -value Get-Extension