Delete All Contents of "FolderName" -recursive

This topic contains 4 replies, has 3 voices, and was last updated by  Curtis Smith 3 weeks, 6 days ago.

  • Author
    Posts
  • #82700

    Jamey
    Participant

    I need to identify all folders that match \Foldername1\Foldername2 under \Users..

    \users has multiple %USERNAME% directories under it.

    Then delete all files under Foldername2

  • #82705

    Curtis Smith
    Participant

    Ok you could use Get-ChildItem to find the directory and Remove-Item to delete it's contents.

  • #82708

    Jamey
    Participant

    THanks, but if I understand correctly Get-ChildItem only spans the current directory. I have to find "Folder3" under

    C:\Users\user1\folder1\folder2\folder3
    C:\Users\user2\folder1\folder2\folder3
    C:\Users\user3\folder1\folder2\folder3
    C:\Users\user4\folder1\folder2\folder3

    Where each username is unique, and then delete the contents of all files in Folder3 for each user. I am also new to PS, so looking for examples ..

    Thanks.

  • #82711

    John Carney
    Participant

    Something like this:

    Get-ChildItem -Path C:\Users\ -Include folder1 -Recurse -Depth 2 -Directory | 
    Get-ChildItem -Directory -Include folder2 | 
    Get-ChildItem -Directory -Include folder3

    Test the output and adjust accordingly; finally pipe the output to Remove-Item.

    There are a few examples on the MS docs page.

  • #82715

    Curtis Smith
    Participant

    You can use wildcards in the path.

    Get-ChildItem -path c:\users\*\folder1\folder2\folder3

    Similarly you could use Resolve-Path.

    Resolve-Path -Path c:\users\*\folder1\folder2\folder3

You must be logged in to reply to this topic.