Converting the FileSystemObject's Size Property

Windows PowerShell

Definition: Returns the file size of a file or the folder size of a folder.


You can use the Get-Item cmdlet with the Length property to retrieve the size of a file:

(Get-Item c:\scripts\test.txt).length

It’s not quite as straightforward to retrieve the size of a folder. To do that, you need to retrieve the length of all files in the folder and add them up.

Here’s how you get the file size of all the files in a folder, not including subfolders:

Get-ChildItem C:\Scripts | Measure-Object -property length -sum

If you’d like to include subfolders, use the -recurse parameter:

Get-ChildItem C:\Scripts -recurse | Measure-Object -property length -sum

See conversions of other FileSystemObject methods and properties.
Return to the VBScript to Windows PowerShell home page