Powershell - script to list the size of users HomeDirectory
GetHomeDirectorySize
At times it might be interesting to have a look at the size of files etc that are stored by the users on the file servers. Specifically if you use Folder redirection etc, and if the free space on the file servers seems to be shrinking at an alarming rate.
What this script does is that it that it picks the HomeDirectory property from the Active directory and scans through it using robocyop.exe. The reason for utilizing the external program instead of using a commandlet ie MeasureObject is that the Windows API has a limit of 260 characters when it comes to folder and file names (ie one folder with a name of 260 chars, or a tree structure spanning over that limit). Hence the use of robocopy.exe in the script.
One caveat was that robocopy does not numerically differ between MB/GB etc, but rather writes out a number ending with “g” for Gigabyte and “m” for Megabyte etc, therefore the script checks the last character of the result and decides if the result it is GB or MB. Rather coarse, but it works.
You can either supply just one username, or a few letters as the script automatically appends a “*“ to the end of the entered value. This is useable if you want to check a span of users in one go. Ie entering ”a” for all user accounts starting with an “a”.
The script is also only listing the users with over 1GB in their home directory. This is by design and the value can be changed to suit your needs.
The script:
[powershell]## Functions
Function LogWrite{
Param ([string]$logstring)
Add-Content $Logfile -Value $logstring
}
### Username, or span of id’s
[string]$filter = read-host "Enter username"
$LogFile = "c:tempSize_homeDirectory_$($filter).txt"
$filter = $filter + "*"
### Gather users from Active Directory
$aduser = Get-ADUser -Filter { cn -like $filter } -properties homedirectory
## Loop through the user(s) and get the total size of the HomeDirectory
foreach ($user in $aduser){
$foldersize = robocopy $user.HomeDirectory NULL /L /S /NFL /NJH /FP /NC /NDL /XJ /R:0 /W:0
# If the result is in MB (m) we’ll skip over it.
if (($foldersize -match "Bytes :" | foreach { $_.ToString().split(” “)[11] }) -notlike "g"){
write-host "$($user.homedirectory) ‘s size is less than 1GB. Skipping.." -foregroundcolor Green
continue
}
$foldersize = $foldersize -match "Bytes :"
$foldersize = $foldersize | foreach { $_.ToString().split(” “)[7] }
if ($foldersize -gt 0){
$foldersize = $foldersize.Replace(".","")
if ($foldersize -gt 1000){
write-host "$($user.name) :" $foldersize
LogWrite "$($user.name) : $($foldersize)"
}
}
}
### License ###
#
#GetHomeDirectorySize
#Copyright (C) 2014 Jonas Andersson
#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
[/powershell]
(Formatting is a tad crappy, sorry about that.)