locked
Deleting only files with 0 KB RRS feed

  • Question

  • I am using the following command below and it's removing all my files it doesn't matter if it has data in it or not. How can I modify the code to only remove files that 0 KB.

    $files=Get-ChildItem "Path*.txt" |where {-not $files -eq 0} |remove-Item

    Thanks,

    • Moved by Just Karl Wednesday, October 18, 2017 9:12 PM Looking for the correct forum
    Tuesday, October 17, 2017 3:06 PM

Answers

  • Add "-File" as directories are always Length=0.


    \_(ツ)_/


    • Edited by jrv Thursday, October 19, 2017 5:41 PM
    • Marked as answer by Richard MuellerMVP Saturday, October 28, 2017 1:39 PM
    Thursday, October 19, 2017 5:40 PM
  • Hi KC_2017.

    This question should be asked in the Official Scripting Guys Forum, which is more appropriate for this kind of topics.
    Your PowerShell snippet should be slightly modified: if you want to remove all the files of any type whose size is 0 you can use the following PowerShell snippet

    Get-ChildItem | Where {$_.Length -eq 0} | Remove-Item


    Bye.


    Luigi Bruno
    MCP, MCTS, MOS, MTA

    Tuesday, October 17, 2017 5:31 PM

All replies

  • Hi KC_2017.

    This question should be asked in the Official Scripting Guys Forum, which is more appropriate for this kind of topics.
    Your PowerShell snippet should be slightly modified: if you want to remove all the files of any type whose size is 0 you can use the following PowerShell snippet

    Get-ChildItem | Where {$_.Length -eq 0} | Remove-Item


    Bye.


    Luigi Bruno
    MCP, MCTS, MOS, MTA

    Tuesday, October 17, 2017 5:31 PM
  • Add "-File" as directories are always Length=0.


    \_(ツ)_/


    • Edited by jrv Thursday, October 19, 2017 5:41 PM
    • Marked as answer by Richard MuellerMVP Saturday, October 28, 2017 1:39 PM
    Thursday, October 19, 2017 5:40 PM