none
How to determine the file size using Powershell script? RRS feed

  • Question

  • I would like to determine whether the file size is greater than 5K or not using Powershell Script.

    $file = "D:\sample.xls"

    If the file size is greater than 5K then True else False.

    Does anyone have any suggestions?

    Any help will be greatly appreciated :>


    Thanks in advance for any suggestions

    Saturday, September 8, 2012 1:57 PM

Answers

  • That seems a little tortured, considering that -gt is going to return $True or $False to start with:

    (Get-Item 'D:\sample.xls').length -gt 5kb


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Saturday, September 8, 2012 2:46 PM
    Moderator
  • You don't need a Try/Catch statement.  This is what you do:

    $file = 'D:\sample.xls'
    if (Test-Path $file) { (Get-Item $file).length -gt 5kb }


    Grant Ward, a.k.a. Bigteddy

    • Marked as answer by oemMicrosoft Sunday, September 9, 2012 2:22 PM
    Saturday, September 8, 2012 4:09 PM

All replies

  • if ((Get-Item 'D:\sample.xls').length -gt 5kb) { $true } else { $false }


    Grant Ward, a.k.a. Bigteddy


    • Proposed as answer by Knuckle-Dragger Saturday, September 8, 2012 2:25 PM
    • Edited by Bigteddy Saturday, September 8, 2012 2:32 PM
    • Unproposed as answer by Knuckle-Dragger Sunday, September 9, 2012 3:08 PM
    Saturday, September 8, 2012 2:13 PM
  • That seems a little tortured, considering that -gt is going to return $True or $False to start with:

    (Get-Item 'D:\sample.xls').length -gt 5kb


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Saturday, September 8, 2012 2:46 PM
    Moderator
  • That seems a little tortured, considering that -gt is going to return $True or $False to start with:

    (Get-Item 'D:\sample.xls').length -gt 5kb


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    Good call, Rob.

    Grant Ward, a.k.a. Bigteddy

    Saturday, September 8, 2012 3:28 PM
  • Does anyone have any suggestions on how to define a variable to catch the statement in case of false?

    Thanks everyone very much for any suggestions

    try {

          (Get-Item 'D:\sample.xls').length -gt 5kb

     }

    catch [System.Exception] {

    }


    Thanks in advance for any suggestions

    Saturday, September 8, 2012 3:30 PM
  • Use test-path to check whether the file exists before trying to get its Length property.

    Bill

    Saturday, September 8, 2012 3:32 PM
    Moderator
  • We all have a synapse misfire occasionally.

    I know I've had my share. :)


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Saturday, September 8, 2012 3:33 PM
    Moderator
  • Use test-path to check whether the file exists before trying to get its Length property.

    Does anyone have any suggestions on how to define variables to catch any following statement in case of false?

    try {

         Test-Path 'D:\sample.xls'

         (Get-Item 'D:\sample.xls').length -gt 5kb

     }

    catch [System.Exception] {

    }

    Thanks everyone very much for any suggestions


    Thanks in advance for any suggestions






    • Edited by oemMicrosoft Saturday, September 8, 2012 3:44 PM
    Saturday, September 8, 2012 3:34 PM
  • You don't need a Try/Catch statement.  This is what you do:

    $file = 'D:\sample.xls'
    if (Test-Path $file) { (Get-Item $file).length -gt 5kb }


    Grant Ward, a.k.a. Bigteddy

    • Marked as answer by oemMicrosoft Sunday, September 9, 2012 2:22 PM
    Saturday, September 8, 2012 4:09 PM
  • Use test-path to check whether the file exists before trying to get its Length property.

    Does anyone have any suggestions on how to define variables to catch any following statement in case of false?

    try {

         Test-Path 'D:\sample.xls'

         (Get-Item 'D:\sample.xls').length -gt 5kb

     }

    catch [System.Exception] {

    }

    Thanks everyone very much for any suggestions


    Thanks in advance for any suggestions






    Try/catch s not triggered by logic.  You seem to have gotten into a rut.  Lately every piece of code that someone gves you you immediately add a try/catch to it event though it does nothing.

    I think you need to go back and study the basics of PowerShell.  It would save you a huge amount of wasted time.


    ¯\_(ツ)_/¯

    Saturday, September 8, 2012 6:06 PM
  • if ((Test-Path $file) -and (Get-Item $file).length -gt 5kb) {break}

    If Test-Path $file is true and (Get-Item $file).length -gt 5kb is true then break

    Do you have any suggestions on whether above statement is correct or not?

    Thanks everyone very much for any suggestions


    Thanks in advance for any suggestions




    • Edited by oemMicrosoft Sunday, September 9, 2012 2:21 PM
    • Proposed as answer by Andry123 Thursday, November 17, 2016 12:37 PM
    Sunday, September 9, 2012 2:09 PM
  • You don't need a Try/Catch statement.  This is what you do:

    $file = 'D:\sample.xls'
    if (Test-Path $file) { (Get-Item $file).length -gt 5kb }

    if ((Test-Path $file) -and (Get-Item $file).length -gt 5kb) {break}

    I try your approach and modify it, and it works now. 

    Thanks everyone very much for any suggestions


    Thanks in advance for any suggestions


    Sunday, September 9, 2012 2:23 PM
  • Neither of those is correct.  If you've tried to run either of them, then you know they aren't correct.

    If you've never even tried them before asking then you're not really trying to figure anything out for yourself.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Sunday, September 9, 2012 2:26 PM
    Moderator
  • Neither of those is correct.  If you've tried to run either of them, then you know they aren't correct.

    If you've never even tried them before asking then you're not really trying to figure anything out for yourself.

    I have tried, and it is working now for my case :>

    What do you find not working?

    Do {

        try {

        ...

            if ((Test-Path $file) -and (Get-Item $file).length -gt 5kb) {break}

        }

        catch [System.Exception] {

        }

    } While ($true)

    Thanks everyone very much for suggestions


    Thanks in advance for any suggestions




    Monday, September 10, 2012 2:22 AM
  • Whatever File path if i give, it is not displaying the length property. So Output is always False.

    What might be the issue here?

    Friday, April 25, 2014 7:21 AM
  • Anyone looking for? Here I wrote an article.

    Check file size using PowerShell

    Hope this article will be helpful.


    Many Thanks,
    Bijay Kumar
    EnjoySharePoint.com | SharePointSky.com | TSInfoTechnologies.com

    Bijay Kumar MVP

    Wednesday, August 1, 2018 7:09 PM
  • This will give you file size in bytes

    (gci $file).Length

    This will give you file size in KB:

    (gci $file).Length/1024 OR (gci $file).Length/1kb

    This will give you file size in MB:

    (gci $file).Length/2048 OR (gci $file).Length/1mb

    and so on...

    If you want to compare, you can do:

    (gci $file).Length -gt 5kb


    Thanks... LH

    Saturday, July 20, 2019 7:12 AM