Determine My Documents folder Size

brianm71
brianm71 used Ask the Experts™
on
We are preparing to move My Documents folders from its local location to a network location.  Before we do this I would like to run a powershell script to access all the machines on my network and return the My Documents folder size for each person on the machine.  Would somebody be able to point me in the right direction?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sander StadSysteemontwikkelaar, Database Administrator

Commented:
This website can help you with that:

http://myitforum.com/cs2/blogs/yli628/archive/2007/05/14/powershell-script-to-check-folder-size.aspx

You have to have an Excel document with all the workstation in it. This can be ip addresses.

Good luck!
PowerShell Developer
Top Expert 2010
Commented:
We could do it like this.

I'm using Quest's CmdLets to find the computers (http://www.quest.com/powershell/activeroles-server.aspx).

Chris
Get-QADComputer | ForEach-Object {
  $Computer = $_

  If ($_.OperatingSystem -Match "2000|2003|XP") {

    Get-ChildItem "\\$($_.Name)\c$\Documents and Settings" | Select-Object `
      @{n='ComputerName';e={ $Computer.Name }},
      @{n='OperatingSystem';e={ $Computer.OperatingSystem }},
      Name, 
      @{n='Owner';e={ (Get-Acl "$($_.FullName)\ntuser.dat").Owner }}, 
      @{n='Documents';e={ "$($_.FullName)\My Documents" }},
      @{n='Size';e={ (Get-ChildItem "$($_.FullName)\My Documents" -Recurse | Measure-Object Length -Sum).Sum / 1Mb }}

  } ElseIf ($_.OperatingSystem -Match "Vista|2008|7") {

    Get-ChildItem "\\$($_.Name)\c$\Users" | Select-Object `
      @{n='ComputerName';e={ $Computer.Name }},
      @{n='OperatingSystem';e={ $Computer.OperatingSystem }},
      Name, 
      @{n='Owner';e={ (Get-Acl "$($_.FullName)\ntuser.dat").Owner }}, 
      @{n='Documents';e={ "$($_.FullName)\Documents" }},
      @{n='Size';e={ (Get-ChildItem "$($_.FullName)\Documents" -Recurse | Measure-Object Length -Sum).Sum / 1Mb }}
  }
}

Open in new window

Commented:
My solution can handle irregular or customized places of the Documents folder and it uses PS 2.0 remoting.
After you define my get-docsize function, it can be called like this:

"computer1", "computer2" | get-docsize | ft docpath, computer, user, size

So it expect computernames as input in the pipeline.
You can get computernames from any source you have: txtfile, get-computer cmdlet from the Active Directory module or Quest's cmdlets.
function get-docsize 
{
begin {
$script = {
dir "hklm:\software\microsoft\windows nt\currentversion\profilelist" | ?{$_.name -match "-(\d+)(\.bak)?$"} | ?{([int] $matches[1]) -ge 1000} |
	%{
		$sid = if($_.name -match "\\(S-1-5-21-[^.]+)(.bak)?$"){$Matches[1]}
		$ntacc = New-Object System.Security.Principal.SecurityIdentifier($sid)
		$acct = $ntacc.translate([System.Security.Principal.NTAccount])
		if(Test-Path "Microsoft.PowerShell.Core\Registry::HKU\$sid"){
			$documents = (Get-ItemProperty -path "Microsoft.PowerShell.Core\Registry::HKU\$sid\software\microsoft\windows\currentversion\explorer\shell folders" -Name personal).personal
		}
		else{
			$profpath = (Get-ItemProperty -Path "Microsoft.PowerShell.Core\Registry::$($_.name)" -Name profileimagepath).profileimagepath
			[void] (invoke-expression "reg load HKU\temp '$profpath\ntuser.dat'")
			$documents = (Get-ItemProperty -path "Microsoft.PowerShell.Core\Registry::HKU\temp\software\microsoft\windows\currentversion\explorer\user shell folders" -Name personal).personal
			$documents = $documents -replace ($env:userprofile -replace "\\","\\"), $profpath
			[void] (invoke-expression 'reg unload HKU\temp')
		}
		$size = 0
		dir $documents -Force -Recurse -ErrorAction SilentlyContinue | %{$size=0} {$size+= $_.length} 
		New-Object -TypeName psobject -Property @{computer = $env:COMPUTERNAME; user = $acct; docpath = $documents; size = $size}
	}
}}
process{
Invoke-Command -ComputerName $_ -ScriptBlock $script
}
}

Open in new window

Commented:
This is even better, if there is a profile for a deleted user, it gives "unknown" as username.
I also refined some parts of it and some little bugs were removed.
So my solution finds the documents folder not only on c drive, but anywhere else.
This new version can be called like this:
"computer1", "computer2" | get-docsize | ft docpath, user, size, pscomputername

if you need sizes in MBs:
"computer1", "computer2" | get-docsize | ft docpath, user, @{n="size in MBs";e={$_.size/1mb};f="n0"}, pscomputername

Of course you need to run it as admin on all computers.
function get-docsize 
{
begin {
$script = {
dir "hklm:\software\microsoft\windows nt\currentversion\profilelist" | ?{$_.name -match "-(\d+)(\.bak)?$"} | ?{([int] $matches[1]) -ge 1000} |
	%{
		$sid = if($_.name -match "\\(S-1-5-21-[^.]+)(.bak)?$"){$Matches[1]}
		$ntacc = New-Object System.Security.Principal.SecurityIdentifier($sid)
		try {$acct = $ntacc.translate([System.Security.Principal.NTAccount])}
		catch {$acct = "unknown"}
		if(Test-Path "Microsoft.PowerShell.Core\Registry::HKU\$sid"){
			$documents = (Get-ItemProperty -path "Microsoft.PowerShell.Core\Registry::HKU\$sid\software\microsoft\windows\currentversion\explorer\shell folders" -Name personal -ErrorAction silentlycontinue).personal
		}
		else{
			$profpath = (Get-ItemProperty -Path "Microsoft.PowerShell.Core\Registry::$($_.name)" -Name profileimagepath).profileimagepath
			[void] (invoke-expression "reg load HKU\temp '$profpath\ntuser.dat'")
			$documents = (Get-ItemProperty -path "Microsoft.PowerShell.Core\Registry::HKU\temp\software\microsoft\windows\currentversion\explorer\shell folders" -Name personal -ErrorAction silentlycontinue).personal
			[void] (invoke-expression 'reg unload HKU\temp')
		}
		dir $documents -Force -Recurse -ErrorAction SilentlyContinue | %{$size=0} {$size+= $_.length} 
		New-Object -TypeName psobject -Property @{user = $acct; docpath = $documents; size = $size}
	}
}}
process{
Invoke-Command -ComputerName $_ -ScriptBlock $script
}
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial