Powershell - Script to list total size of files in HomeDirectory

HomeDirFileSize

Following up on the POWERSHELL – SCRIPT TO LIST THE SIZE OF USERS HOMEDIRECTORY script posted earlier this script goes through the users HomeDirectory and tries to sum up the total size of the files based on their file type.
It is not intended to give an exact measurement, there are other applications out there for that, but a good estimate of what kind of files it is that takes up space on the file server.
There are a few variables where some common file types are specified, and you can of course adapt them after your own needs.

The script:

Import-Module ActiveDirectory  
 ## Functions

Function LogWrite{  
 Param ([string]$logstring)  
 Add-Content $Logfile -Value $logstring  
 }

### Variables ###  
 $VideoFiles = @(“.mpg”,”.mpeg”,”.mp4″,”.mts”,”.wmv”,”.avi”,”.flv”)  
 $AudioFiles = @(“.mp3″,”.wav”,”.wma”,”.ogg”,”.flac”)  
 $PictureFiles = @(“.jpg”,”.jpeg”,”.gif”,”.bmp”,”.tif”,”.png”)

### Enter username  
 [string]$filter = read-host “Enter username”  
 $LogFile = “c:tempHomeDirFileSize_$($filter).txt”

### Fetch user from Active Directory  
 $user = Get-ADUser -Filter { cn -like $filter } -properties homedirectory

## Clear variables ##  
 [int]$PictureSizeTotal =””  
 [int]$PictureCount =””  
 [int]$VideoSizeTotal =””  
 [int]$VideoCount =””  
 [int]$AudioSizeTotal =””  
 [int]$AudioCount =””  
 [int]$MiscFilesSize =””  
 [int]$MiscFileCount =””

## Loop through the files and sort them according to type.

foreach ($file in (Get-ChildItem -Recurse -Path $user.HomeDirectory -ErrorAction SilentlyContinue)){

if ($PictureFiles -contains $file.extension){  
 #write-host “Picture files found!” -ForegroundColor Green  
 $PictureSizeTotal = $PictureSizeTotal + ([math]::truncate($file.length / 1MB))  
 $PictureCount = $PictureCount + 1  
 continue  
 }

if ($VideoFiles -contains $file.extension){  
 #write-host “Video files found!” -ForegroundColor yellow  
 $VideoSizeTotal = $VideoSizeTotal + ([math]::truncate($file.length / 1MB))  
 $VideoCount = $VideoCount + 1  
 continue  
 }

if ($AudioFiles -contains $file.extension){  
 #write-host “Audio files found!” -ForegroundColor cyan  
 $AudioSizeTotal = $AudioSizeTotal + ([math]::truncate($file.length / 1MB))  
 $AudioCount = $AudioCount + 1  
 continue  
 }

#write-host “Misc file found!” -foregroundcolor DarkRed  
 $MiscFilesSize = $MiscFilesSize + ([math]::truncate($file.length / 1MB))  
 $MiscFileCount = $MiscFileCount +1  
 continue

}

LogWrite “Pictures: $PictureSizeTotal ; Soundfiles: $AudioSizeTotal ; Videofiles: $VideoSizeTotal ; Misc Files: $MiscFilesSize”

### License ###  
 #<one line to give the program’s name and a brief idea of what it does.>  
 #Copyright (C) <year> <name of author>

#This program is free software; you can redistribute it and/or modify  
 #it under the terms of the GNU General Public License as published by  
 #the Free Software Foundation; either version 3 of the License, or  
 #(at your option) any later version.  
 #  
 #This program is distributed in the hope that it will be useful,  
 #but WITHOUT ANY WARRANTY; without even the implied warranty of  
 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  
 #GNU General Public License for more details.  
 #  
 #You should have received a copy of the GNU General Public License  
 #along with this program; if not, write to the Free Software Foundation,  
 #Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA  
 #  
 #https://gnu.org/licenses/gpl.html  
Mastodon