Author Posts

August 19, 2016 at 6:29 pm

Hello, I've been tasked with getting file share info such as name, actual path, and space used in MB or GB. We have two many servers for me to continue to do this manually so I started writing a powershell script but have run into some issues.

Here's the code so far:

$shares = gwmi Win32_Share | ? {$_.Description -eq "$null"}
foreach($share in $shares){
$size = (Get-childItem -Path $share.Path | Measure-object -Property length -sum)
write-host $share.Name $share.Path ($size.sum / 1GB -as [int])
}

I don't want the default shares hence the {$_.Description -eq "$null"}. It works just fine if there's only one share, but if there are multiple I get this:

Measure-object : Property "length" cannot be found in any object(s) input

Any ideas how to fix this?

August 19, 2016 at 6:55 pm

Add -File to your Get-ChildItem. The problem is that you're getting Files and Directories, and Directory objects don't have a length.

August 19, 2016 at 6:58 pm

Also... and perhaps obviously... expect this to not be fast. And to possibly fail in any instance where you don't have permission to read the file. Or when the server just gets tired of sending file data to you and drops the connection. This is something I'd probably write as a workflow, ship to the server via Remoting, and let each server run locally on its own. They'd then ship the results back to you however many hours later.

But that's just me :).

August 19, 2016 at 6:58 pm

Oh, you'll need -Recurse on Get-ChildItem, too, or you're only going to get the files in the root of the share. Remember, directories don't have sizes – only files.

August 19, 2016 at 7:43 pm

Hm, can't seem to get it working with -File and -Recurse either. We're running different versions of Windows Server so I don't think the Powershell is the same across them. I tried Get-ChildItem with -File and it said that wasn't a parameter. Grr this is getting frustrating, anything besides powershell to do this? haha.

August 19, 2016 at 8:04 pm

Oh. You have a VOVOPS, or Very Old Version of PowerShell. You'll have to pipe Get-ChildItem to Where-Object to remove all but the files.

gci . *.* -rec | where { $_.GetType().Name -eq "FileInfo" }

-Recurse (-rec) should work regardless. It's been there since 1.0.

And yes, there are lots of tools that do what you're after. Mostly commercial ones that are really fast at it, too.

August 19, 2016 at 8:27 pm

Before the -File parameter became available on the Get-Childitem cmdlet I've used the property PSIsContainer to filter for files only.

gci . *.* -rec | where { -not $_.PSIsContainer }