Updates from March, 2008 Toggle Comment Threads | Keyboard Shortcuts

  • Chris 12:18 pm on March 24, 2008 Permalink | Reply
    Tags: , Home Directories,   

    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 ($user.name)"&"($Displayname)"&"($user.HomeDirectory)"&"([int]($folder.Size/$conversion_factor))"&"($user.useraccountcontrol)
    }<Create the conversion factor to make it megabytes
    $CONVERSION_FACTOR = 1048576

    #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 = $Folder.name; 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 .

    $CONVERSION_FACTOR = 1048576

    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 = $Folder.name; 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 = $Folder.name . (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 ($user.name)”&;”($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 ($user.name) , 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 !


    • halr9000 4:32 pm on March 26, 2008 Permalink

      Chris, did you know you can just use ‘1MB’ as a constant for the conversion factor? Works for kb, and gb as well.


    • Chris 1:19 pm on March 27, 2008 Permalink

      Thanks Hal


  • Chris 9:09 pm on March 9, 2008 Permalink | Reply
    Tags: , Tech Podcasts   

    I won a copy of Powershell Analyzer 

    Hi Folks ,

     I have not been blogging much . I have though been listening to a lot of Podcasts which are really cool . These guys do it for free which is very impressive being that they all have busy lives working in the IT industry and for them to take the time to create these podcasts is remarkable in itself .

    Here are the podcasts I listen too :




    They are all on itunes if you do a search you will see them . I recently won a prize ( I never win anything ) from the cmdlet contest they had at powerscripting and CoupleofAdmins podcasts . Its called Powershell Analyzer http://www.powershell.com/analyzer/

    Its an IDE environment for powershell .Check it out .

    So many things have been happining . Windows 2008 and Vista SP1 have been released . I have just upgraded my Home network to Windows 2008 and it is awesome .SQL Server 2008 has been delayed . The Winter Scripting games 2008 Games have finished . I competed in 3 beginner games . I wish I had the time to do more but work got in the way ( I hate that ) . I have been  looking for a new Job which has been taking up my time as well .

    That’s why I have not been posting allot . I also been thinking about getting my own website . This blog is great but I would like more control over it and have storage space to do what I like to do . Well that’s it for now .


Compose new post
Next post/Next comment
Previous post/Previous comment
Show/Hide comments
Go to top
Go to login
Show/Hide help
shift + esc