Powershell delete subfolders with a specific name

Question:

I’m new to PowerShell and I have been trying create a script but so far, I have been unsuccessful. I want it to do a specific task which is ;

I need to be able to search through a drive for a specific folder called "Cookies" and delete it. The problem only is the folder cookies is set at multiple locations.

Example :

and go on…

How do I get powershell to go trough all those different USER folder to search for the Cookies folder and delete it.

I came up with this, but I was hoping a powershell guru could help me.

Will this work ?

Answer:

You are almost there. No need to use quotes around $cookies.

If you do a foreach ($cookie in $cookies), then operate on $cookie in the script block, not $cookies.

This works:

but this will work as well, without a loop:

If you want a one-liner and no variables:

Source:

Powershell delete subfolders with a specific name by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply