Use PowerShell to find the free space on the volume behind an SMB file share

December 25, 2012

in Microsoft,PowerShell,PowerShell V3,Window Server 2012,Windows 8

Jose Barreto blogged the details HERE.

His original script is fine and works.

I refactored his script this way. I prefer this style, it feels more readable. Using features of PowerShell v3 makes it easier to write scripts more concisely. Jose used PowerShell v2 techniques that still work but are more verbose and bulky.

What do you think?

Refactored Version

$CimSession = "."            

ForEach($item in Get-SmbShare -CimSession $CimSession | Where Volume) {            

    $volume = Get-Volume -CimSession $CimSession -Id $item.Volume            

    [PSCustomObject] @{            
        Share     = $item.Name            
        Path      = $item.Path            
        Size      = $volume.Size            
        Remaining = $volume.SizeRemaining             
    }            
}

Original

Get-SmbShare -CimSession FST2-FS2 |             
    ? Volume  -ne $null |             
    % {             
        $R = "" | Select Share, Path, Size, Free            
        $R.Share=$_.Name; $R.Path=$_.Path            
        Get-Volume -CimSession FST2-FS2 -Id $_.Volume |             
        % {             
            $R.Size=$_.Size; $R.Free=$_.SizeRemaining; $R | FL }            
        }

{ 4 comments… read them below or add one }

Michael Koehler 02.18.13 at 8:35 pm

I like this but must offer a short script I have been using for awhile. I wish I could say who I learned it from.

function Get-Disk { $fso = new-Object -com Scripting.FileSystemObject; $fso.getdrive($args) }

Usage:

Get-Disk c:
Get-Disk \\server\share

will return an object like:

Path : C:
DriveLetter : C
ShareName :
DriveType : 2
RootFolder : System.__ComObject
AvailableSpace : 60581060608
FreeSpace : 60581060608
TotalSize : 255505461248
VolumeName :
FileSystem : NTFS
SerialNumber : 144432260
IsReady : True

Doug Finke 02.19.13 at 10:17 pm

Thanks for the PowerShell script. Great way to skin the cat.

Doug

Chris Warwick 02.20.13 at 4:49 am

You have a bug: $Target should be $CimSession

Also, you can improve this by removing duplicate volumes:

$CimSession = ‘.’
Foreach ($item in Get-SmbShare -CimSession $CimSession |
Where Volume |
Sort -Unique Volume
) {

$volume = Get-Volume -CimSession $CimSession -Id $item.Volume

[PSCustomObject] @{
Share = $item.Name
Path = $item.Path
Size = $volume.Size
Remaining = $volume.SizeRemaining
}
}

Doug Finke 02.20.13 at 9:08 pm

Thanks Chris for catching the bug and the optimization. I corrected the code.

Doug

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Contrat Creative Commons

© 2007-2013, Doug Finke