Powershell – Why can’t I get the size of network shared folder? by powershell

network-sharepowershell

I've already know a sharespace path: \sharespace\test1

I used this line to get the latest update folder under this path:

$lastbuild=get-childitem $var.droppath.TrimEnd()|where-object {$_.name -match "10."} |Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1
$lastbuild.fullname.TrimEnd()

what I got is \\sharespace\test1\10.0.1212.1

How can I get this folder's size? I used the following code, but only get the totalsize of its parent path\\sharespace\test1

$mapbuild.mapNetworkDrive("u:",$lastbuild.fullname.TrimEnd())

$fsobuild = new-Object -com Scripting.FileSystemObject

$dobuild = $fsobuild.getdrive("u:")

Write-host "Size of latestbuild:"

$dobuild.totalsize

Best Answer

This looks like exactly what you need: Scripting Guy article on getting folder size

I think it's important to note that powershell can typically handle UNC paths natively, so there's no need to map a drive. You should be able to do something like this:

$fsobuild = new-Object -com Scripting.FileSystemObject
"{0:N2}" -f (($fsobuild.GetFolder($lastbuild.Fullname).Size) / 1MB) + " MB"