locked
PowerShell Deleteing Files and Folders, but should only delete files. RRS feed

  • Question

  • Ok, when I run the following command on a test folder, it works great.

    Get-ChildItem –Path "z:\testfolder" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-7))} | Remove-Item

    When I change to the "production" folder, and run the same command, I get the following error:

    Get-ChildItem –Path "z:\PRDSQLBackups" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-7))} | Remove-Item

    The item at Microsoft.PowerShell.Core\FileSystem::Z:\PRDSQLBackups\PRDSQLGR\SQLDB-Logs has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure y
    want to continue?
    [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

    I don't know why I am getting a prompt, and it looks like it will delete the FOLDERS as well. I don't want the folders deleted. The testfolder is the exact same folder as the production folder. I copied it so that I could run the test on it. It looks like it would delete everything.

    What am I doing wrong?

    Tuesday, June 5, 2018 3:17 PM

All replies

  • Get-ChildItem –Path "z:\testfolder" -Recurse  -File

    \_(ツ)_/

    • Proposed as answer by SYSKO company Wednesday, June 6, 2018 7:06 AM
    Tuesday, June 5, 2018 3:20 PM
  • I do not know what this means....

    \_(ツ)_/

    Tuesday, June 5, 2018 3:25 PM
  • What is it that you don't know?  You need to use only the files.  The default is all files and directories.


    \_(ツ)_/

    Tuesday, June 5, 2018 3:32 PM
  • Ok, then how do I do that?

    Tuesday, June 5, 2018 3:35 PM
  • How do you do what?


    \_(ツ)_/

    Tuesday, June 5, 2018 3:45 PM
  • Remove only the files?
    Tuesday, June 5, 2018 3:49 PM
  • In the test folder, it doesn't remove the folders...

    So why is it removing folders when it runs from the production folder?

    Tuesday, June 5, 2018 3:52 PM
  • You need to use the version of the command that I posted.


    \_(ツ)_/

    Tuesday, June 5, 2018 3:56 PM
  • I see it now.. -File.. sorry... just didn't see it when you first posted. My bad...

    It has been a very long day already...

    Tuesday, June 5, 2018 4:05 PM