You’d think this wouldn’t be that hard of a task. However, I spent about an hour wading through misinformation and poor code snippets to figure this out.
To determine a file’s size from within PowerShell, use the following snippet:
$var = Get-Item [filename] $var.length
In my specific case, I used:
$file = Get-Item .g2mdlhlpx.exe $file.length/1KB 70.390625
So why all the confusion? After a right thorough search of Google, I came back with what I eventually figured out was some pretty crazy things. One was even from a Microsoft MVP who said to load a variable with files thusly:
$var = "file.txt"
The problem with that was that executing ‘$var.length’ always returned “8”. See why? I eventually figured out that it was because I was feeding the variable a string. I know, this is scripting 101. No, this is scripting 001. I’m a Windows admin. Cut me some slack.
The secret is in the use of the cmdlet Get-Item. Huzzah! It was the good folks at #PowerShell on FreeNode (Thanks BartekB!!) that dispelled my confusion in seconds flat.
I can only hope that my blog post will make it’s way up the search engine rankings to dispel some of the confusion.








About the Author
I am a self employed Systems Administrator in sunny Phoenix, Arizona. I like origami and lemons. I do not like hangnails or snow. Interested in hiring me? Check the blog sidebar to the right for a contact form or email me at [email protected]