Display a file’s size in kilobytes using Windows PowerShell

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]

In my specific case, I used:

$file = Get-Item .g2mdlhlpx.exe

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.


  1. Cobra

    March 17, 2011 at 8:08 am

    Brilliant powershell code~!! It worked….even I was searching on the internet didn’t find anything, on how to find the size of a single file …finally you saved me…Thx a Ton!!


  2. JO

    March 30, 2011 at 5:53 am

    It works well.. 🙂


  3. Parag

    July 14, 2011 at 12:07 am

    Thank you very much.You saved me a lot of time and efforts 🙂


    • Wesley David

      July 14, 2011 at 11:28 am

      You’re very welcome, Parag. It took me too long to figure out how to do this so I wanted to share and hopefully save someone else the time. =)


Leave a Reply

Your email address will not be published. Required fields are marked *

Follow TheNubbyAdmin!

follow us in feedly

Raw RSS Feed:

Contact Me!

Want to hire me as a consultant? Have a job you think I might be interested in? Drop me a line:

Contact Me!

Subscribe via Email

Your email address is handled by Google FeedBurner and never spammed!

The Nubby Archives

Circle Me on Google+!

Photos from Flickr

Me on StackExchange

The IT Crowd Strava Group