Script: List users homedir size

I compiled a vbscript that loops through all the sub folders of a given folder and print out the size. The script takes the folder name and searches the Active Directory for a corresponding user. If found it prints the name of the user in the output.

Use:

[ps]cscript homedirsize.vbs[/ps]

Three input boxes will appear…

  1. Path where the homedir folders are located. Ex. d:home
  2. Width, in characters, of the first output column.
  3. Width, in characters, of the second output column.

Download script here: [wpdm_file id=2]
[vb]
‘****************************************************************************************

‘ Name: Homedirsize.vbs

‘ Retrieves the size of each subdirectory and matches them to an AD account.
‘ Outputs a list of directories, username and size.

‘ Written by: Kristofer Källsbo
‘ 2013-02-26 – http://www.hackviking.com

‘****************************************************************************************
Option explicit
dim path, column1, column2, objRoot, domainname, fso, partline, i, rootFolder, folder

‘ get path of homedirs
path = inputbox("Enter path of homedirs:")

‘ get column widths
column1 = Cint(inputbox("Enter width of first output column:"))
column2 = Cint(inputbox("Enter width of second output column:"))

‘ get the current domain
Set objRoot = GETOBJECT("LDAP://RootDSE")
domainname = objRoot.GET("defaultNamingContext")

‘ get the file system object
Set fso = CreateObject("Scripting.FileSystemObject")

‘ print description lines
wscript.echo "homedirsize.vbs runned on " & Date & " – " & Time
wscript.echo ""
wscript.echo LeftJustified("Foldername", column1) & LeftJustified("Username", column2) & "Size (Mb)"

for i = 0 to column1 + column2 + 8
partline = partline & "-"
next

wscript.echo partline

‘ start looping all the subfolders
Set rootFolder = fso.GetFolder(path)
For Each folder in rootFolder.SubFolders
Dim folderSize
folderSize = folder.Size

wscript.echo LeftJustified(folder.Name, column1) & LeftJustified(FindUser(folder.Name, domainname), column2) & FormatNumber(((folderSize/1024)/1024),2) & " Mb"
Next

Set fso = Nothing

FUNCTION FindUser(BYVAL UserName, BYVAL Domain)
Dim cn,cmd,rs
SET cn = CREATEOBJECT("ADODB.Connection")
SET cmd = CREATEOBJECT("ADODB.Command")
SET rs = CREATEOBJECT("ADODB.Recordset")

cn.open "Provider=ADsDSOObject;"

cmd.activeconnection=cn
cmd.commandtext="SELECT Name FROM ‘LDAP://" & Domain & _
"’ WHERE sAMAccountName = ‘" & UserName & "’"

SET rs = cmd.EXECUTE

IF err<>0 THEN
FindUser = 2
wscript.echo "Error connecting to Active Directory Database:" & err.description
ELSE
IF NOT rs.BOF AND NOT rs.EOF THEN
rs.MoveFirst
FindUser = rs.Fields("Name").Value
ELSE
FindUser = "N/A"
END IF
END IF
cn.close
END FUNCTION

Function LeftJustified(ColumnValue, ColumnWidth)
If(ColumnWidth < Len(ColumnValue) OR ColumnWidth = Len(ColumnValue)) then
LeftJustified = Left(ColumnValue, ColumnWidth – 1) & " "
else
LeftJustified = ColumnValue & Space(ColumnWidth – Len(ColumnValue))
End if
End Function
[/vb]

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: