Doing Some File info gathering in Powershell for Home Directories

I haven’t written anything in awhile and I apologize for that . This blog though should be interesting . Have you ever wanted to run a report on lets say How much disk usage your users are using for there home directory or maybe want to echo back the users H drive path from Active Directory . This information can be helpful in many ways . It can verify that the uses home directory path in ad is pointing to the correct path . How do you do this ? Well most likely your users home directory is the name of the User account ID in Active Directory .What you want to do is get all the directories in for example \\servername\homedirs\then go thru each directory name and basically connect to AD to verify Home directory path . You may also want to see if the account is disabled or not . Lets face it all this info is useful . Users come and go they move around so having some kind of reporting is a good thing . You do not need fancy software to do it either which will make your boss happy 🙂 .

I’m going to show you the full script then talk about it alittle :

#*ScriptName: Verify-ADUserProfile

  1. *Date : 03/06/2008


function AdUserProps($UserName,[object]$folder)
$root = New-Object system.DirectoryServices.DirectoryEntry("LDAP://dc=Microsoft,dc=com")
$search = New-Object system.DirectoryServices.DirectorySearcher($root)
$search.Filter = "(&(objectcategory=user)(saMAccountname=" + $UserName+ "))"
$found = $search.findOne()
$user = New-Object system.DirectoryServices.DirectoryEntry($found.path)
$Displayname = $user.Displayname
write-host ($"&"($Displayname)"&"($user.HomeDirectory)"&"([int]($folder.Size/$conversion_factor))"&"($user.useraccountcontrol)
}<Create the conversion factor to make it megabytes

#create a FileSystem Object
$fsO = New-Object -ComObject ("Scripting.FileSystemObject")


#create the first Pipeline to get the path of all Directories listed
$homdirs = Get-ChildItem "\\ServerName\homedir$" |?{$_.PsIsContainer} | % {$_.FullName} | % { $folder = $fso.getfolder($_) ;$UserName = $; AdUserProps $UserName $folder }

Now this is not a very hard script it outputs to the screen but you could pipe it to a text file . The text fields are split up by a & character kind of like a comma delimitted file . So you can take the info and import it into excel .

Lets go thru the script :

Lets skip the function at the top for now we will get back to it later .


This is a conversion to present the data in Megabytes . Believe me its much easier to look at this way then using bytes .

#create a FileSystem Object
$fsO = New-Object -ComObject (“Scripting.FileSystemObject”)

Here he define our com object (Filesystem) because we want to know the size of the directories . So we instantiate it here so we can use it later .


This is top line of the text file . Kind of like a header file for the excel sheet . You notice that it is broken up with the & character to easily import it into excel later .

#create the first Pipeline to get the path of all Directories listed
$homdirs = Get-ChildItem “\\ServerName\homedir$” |?{$_.PsIsContainer} | % {$_.FullName} | % { $folder = $fso.getfolder($_) ;$UserName = $; AdUserProps $UserName $folder }

This is the bread and butter . (Note : this is one pipeline . One Line . Sorry for the formatting . ) Lets take a close look at this pipeline .

We have our variable $homdirs = get-childitem . We get all the files and folders under our path \\servername\homedir$ .Notice that I didn’t use recursive because I’m not worried about sub-directories . This gets piped | to ?{$_.PsIsContainer} which gets only the directories. This goes into a foreach-object (%) pipe to get the Fullname which is the Full Path . The next bit is interesting . I make $folder variable = to our $fso which remember is our Filesystem object . I use the getfolder method which we will use when we call our function later . Now the next part doesn’t get piped $UserName = $ . (I will have to look at this again but because it may seem like I’m creating the same thing over again but basically we are creating the 2 variables $Folder and $username which we will call the function AdUserProps) .

This function returns all the info we are looking for . Now there are many ways to connect to Active directory . you can use the way you feel most comfortable . I use the LDAP method but you can use Quests AD cmdlets too .

$root = New-Object system.DirectoryServices.DirectoryEntry(“LDAP://dc=Microsoft,dc=com”)
$search = New-Object system.DirectoryServices.DirectorySearcher($root)
$search.Filter = “(&;(objectcategory=user)(saMAccountname=” + $UserName+ “))”
$found = $search.findOne()
$user = New-Object system.DirectoryServices.DirectoryEntry($found.path)
$Displayname = $user.Displayname
write-host ($”&;”($Displayname)”&;”($user.HomeDirectory)”&;”([int]($folder.Size/$conversion_factor))”&;”($user.useraccountcontrol)

What us happening here is I’m conneting to the root of the domain and searching for the Username that I passed in . Remember we don’t know where these users are in AD . Once I find the User name I display its userID ($ , the Display Name $user.Displayname,Users Home directory $user.HomeDirectory

int]($folder.Size/$conversion_factor — here is where we use the $folder variable to figure out what size the Directory is .

$user.useraccountcontrol — Last but not least if the account is Disabled or not . 512 is Active , A 514 is disabled .

Thats the gist of it . I hope this gives you some idea’s on how to account for Homedirectories . Lets face it Cleanup is not a fun part of the Job but if you are able to get some info it could help lead you in the right path .

Hope this helps !