• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 609
  • Last Modified:

getting folder size of home directories

I have this script here that pulls info for user out of AD. The last thing I am trying to work out is getting the total size of the users home directory. Any help with this script here would be very much appreciative, thanks.

$filter = "(&(objectcategory=user)(memberof=CN=somegroup,DC=something,dc=com))"
$ds = New-Object System.DirectoryServices.DirectorySearcher([ADSI]"",$filter)
$ds.pagesize = 1000
$ds.Findall() |
Select-Object `
    @{n='Name';e={ $_.Properties['name'][0] }},
    @{n='homedirectory';e={ $_.Properties['homedirectory'][0] }},
    @{n='LastLogonTimeStamp';e={ [DateTime]::FromFileTime($_.Properties["lastlogontimestamp"][0]) }},
    @{n='OU';e={ $_.Properties['distinguishedname'][0] -Replace "CN=$($_.Properties['cn'][0])," }} |
   
    export-csv info.csv -noTypeInformation
0
techdrive
Asked:
techdrive
  • 4
  • 3
1 Solution
 
gcitronCommented:
Try to add this column in select-object:  
@{n='homedirectorysize';e={  (Get-ChildItem $_.Properties['homedirectory'][0] -recurse | Measure-Object -property length -sum ).sum }}
0
 
techdriveAuthor Commented:
this did not work. Did you test this?
0
 
gcitronCommented:
I tested in the form:
$env:userprofile | select @{n='homedirectorysize';e={  (Get-ChildItem $_ -recurse | Measure-Object -property length -sum ).sum }}

In the homeDirectory property is the local path for userprofile, so this script is usable on a TS, where you get homedirectorysize for all profiles on that server. If you want homedirectorysize on workstations, then you must know on what workstations the user has a profile and try something like
("\\$computername\" + ($_.Properties['homedirectory'][0]  -replace ":", "$"))
instead of $_.Properties['homedirectory'][0] ...
0
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

 
techdriveAuthor Commented:
These directory sizes are not on workstations, they are on the network. This is pulling from the home directory attribute in AD. I was just wondering if there was a way to calculate the folder size being presented in the select object statement.
0
 
gcitronCommented:
Ok, that information is stored as I konw in "profilePath", not in "homeDirectory". Anyway, if your script display the path like "\\server\share\path\username", you can calculate the size of that folder with (Get-ChildItem "\\server\share\path\username" -recurse | Measure-Object -property length -sum ).sum .
0
 
techdriveAuthor Commented:
Yes, understood and if I were doing this as a seperate piece it would work fine. My question is how do I incorporate this into the script above.
0
 
gcitronCommented:
$filter = "(&(objectcategory=user)(memberof=CN=somegroup,DC=something,dc=com))"
$ds = New-Object System.DirectoryServices.DirectorySearcher([ADSI]"",$filter)
$ds.pagesize = 1000
$ds.Findall() |
Select-Object `
    @{n='Name';e={ $_.Properties['name'][0] }},
    @{n='homedirectory';e={ $_.Properties['homedirectory'][0] }},
    @{n='homedirectorysize';e={  (Get-ChildItem $_.Properties['homedirectory'][0] -recurse | Measure-Object -property length -sum ).sum }},
    @{n='LastLogonTimeStamp';e={ [DateTime]::FromFileTime($_.Properties["lastlogontimestamp"][0]) }},
    @{n='OU';e={ $_.Properties['distinguishedname'][0] -Replace "CN=$($_.Properties['cn'][0])," }} |
   
    export-csv info.csv -noTypeInformation
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now