Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we get the size of the file in KB using powershell scripting?

Tags:

powershell

I have tried $filesize=(($fileXP).length/1KB) but this does not retrieve the correct answer. As for my understanding, it is just dividing the number of characters in files with KB and giving the answer which is wrong.

There has to be some function or attribute to define the size directly and display as we want in KB or MB or GB as required.

like image 568
Svatkat Avatar asked Jun 27 '16 11:06

Svatkat


1 Answers

To get the file sizes in KB:

Get-ChildItem | % {[int]($_.length / 1kb)}

Or to round up to the nearest KB:

Get-ChildItem | % {[math]::ceiling($_.length / 1kb)}

To get the number characters:

Get-ChildItem | % {(Get-Content $_.FullName).length}

Compare the output and you will see that the length of Get-Childitem is in KB.

like image 197
Dave Sexton Avatar answered Oct 11 '22 11:10

Dave Sexton