locked
Remove only a specific subfolder located in serveral folders RRS feed

  • Question

  • In PowerShell, I try to do a script that will remove a specific subfolder located in our users profils.

    For example, I want to remove the subfolder named "Logs" located in "DriveLetter\RoamingProfils\%UserName%\EnterpriseApp".

    But I don't want this script to refer to a list of all users for populating "%UserName%".

    I just want this script to remove all "EnterpriseApp\Logs" folders.

    Here the code I have today :

    Remove-Item -Path C:\RoamingProfils* -Filter "*\EnterpriseApp\Logs"

    But an error message informs me that there are non conform caracters in the path.

    How should I do ?

    Thank you !


    FXE

    Tuesday, January 26, 2016 4:59 PM

Answers

  • I want the script to search all the folders named "EnterpriseApp\Logs" in "C:\RoamingProfils".

    How many levels down? If it's only one (and consistently one), you can do something like this:


    Get-ChildItem C:\RoamingProfils\*\EnterpriseApp\Logs


    • Marked as answer by FXE Wednesday, January 27, 2016 9:13 AM
    Tuesday, January 26, 2016 5:18 PM

All replies

  • First, Use Get-ChildItem to find the files you want to remove. Make sure your Get-ChildItem command outputs only the desired files.

    Then, pipe to Remove-Item to remove the files.

    For example:


    PS C:\> Get-ChildItem C:\RoamingProfiles\$Env:USERNAME\EnterpriseApp\Logs\*

    If this shows the correct files, you can pipe to Remove-Item:


    PS C:\> Get-ChildItem C:\RoamingProfiles\$Env:USERNAME\EnterpriseApp\Logs\* | Remove-Item -WhatIf

    Of course, remove the -WhatIf to actually remove the files.

    Also: Note that PowerShell does not use %USERNAME%. In PowerShell, we write $Env:USERNAME instead.


    -- Bill Stewart [Bill_Stewart]


    Tuesday, January 26, 2016 5:06 PM
  • Bill, thank you for your answer.

    OK, but I don't want to use an environement variable.

    I want the script to search all the folders named "EnterpriseApp\Logs" in "C:\RoamingProfils".

    Regards,


    FXE

    Tuesday, January 26, 2016 5:11 PM
  • I want the script to search all the folders named "EnterpriseApp\Logs" in "C:\RoamingProfils".

    How many levels down? If it's only one (and consistently one), you can do something like this:


    Get-ChildItem C:\RoamingProfils\*\EnterpriseApp\Logs


    • Marked as answer by FXE Wednesday, January 27, 2016 9:13 AM
    Tuesday, January 26, 2016 5:18 PM
  • Hi all,

    Thank you for you help Mike and Bill.


    FXE

    Wednesday, January 27, 2016 9:14 AM
  • Cheers, you're very welcome.

    Wednesday, January 27, 2016 12:40 PM