5

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 :

\\\myserver\test\User\Profiles\USER1\AppData\Roaming\Microsoft\Windows\Cookies
\\\myserver\test\User\Profiles\USER2\AppData\Roaming\Microsoft\Windows\Cookies
\\\myserver\test\User\Profiles\USER3\AppData\Roaming\Microsoft\Windows\Cookies
\\\myserver\test\User\Profiles\USER4\AppData\Roaming\Microsoft\Windows\Cookies

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.

$cookies= Get-ChildItem \\myserver\test\User\Profiles\*\AppData\Roaming\Microsoft\Windows\Cookies

foreach ($cookie in $cookies){
    Remove-Item "$cookies" -Force -Recurse -ErrorAction SilentlyContinue   
}

Will this work ?

Roy
  • 53
  • 1
  • 6

3 Answers3

6

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:

$cookies = Get-ChildItem \\myserver\test\User\Profiles\*\AppData\Roaming\Microsoft\Windows\Cookies

foreach ($cookie in $cookies){
    Remove-Item $cookie -Force -Recurse -ErrorAction SilentlyContinue   
}

but this will work as well, without a loop:

$cookies = Get-ChildItem \\myserver\test\User\Profiles\*\AppData\Roaming\Microsoft\Windows\Cookies
Remove-Item $cookies -Force -Recurse -ErrorAction SilentlyContinue

If you want a one-liner and no variables:

Get-ChildItem \\myserver\test\User\Profiles\*\AppData\Roaming\Microsoft\Windows\Cookies | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
  • @Roy Working with the pipeline (last example) is always preferable, when possible. – marsze Jan 17 '19 at 10:55
  • @Hoi Gert Jan, Thanks for this, but our security admin wants a small change to the code. Which is to remove .txt files from the cookie folder that are older then 60 days but i'm getting a memory error. `get-item -path D:\User\Profiles\*\AppData\Roaming\Microsoft\Windows\Cookies\* -filter "*.txt" -force | Where-Object {$_.LastWriteTime -lt (get-date).addDays(-60) }` – Roy Jan 17 '19 at 11:01
  • @Roy Your code looks alright to me. Are there a lot cookie files to collect there? – Gert Jan Kraaijeveld Jan 17 '19 at 11:19
  • Nicely done, though you should use `Remove-Item $cookie.FullName`, because the implied `-Path` parameter (ditto for `-LiteralPath`) binds its argument as a _string_, and in Windows PowerShell `Get-ChildItem` _situationally_ stringifies to the _mere filename_, which causes the command to either fail or, worse, delete the wrong item. It won't happen with this particular `Get-ChildItem` call, but always using `.FullName` when passing `Get-ChildItem` / `Get-Item` as an _argument_ is a good habit to form (pipeline input is not affected). See https://stackoverflow.com/a/53400031/45375 for more. – mklement0 Jan 17 '19 at 12:08
  • Yeah I have around 700 users and every one of them has around 4000 or more cookies... – Roy Jan 17 '19 at 12:09
  • @Roy Then make sure you do not store all those objects in a variable, but use a proper pipelined procedure, like the one-liner above – Gert Jan Kraaijeveld Jan 17 '19 at 12:13
0

Try this one liner -

$path = "\\myserver\test\User\Profiles\"
Get-ChildItem $path -Recurse -Directory | Where-Object {$_.Name -eq 'Cookies'} | % {Remove-Item $_.FullName}
Vivek Kumar Singh
  • 3,223
  • 1
  • 14
  • 27
  • Let me try that, i'll keep you posted – Roy Jan 17 '19 at 10:15
  • Hi Vivek, i've changed the code a little bit because we want to have a code delete the cookie files which are .txt files and they need to be older then 60 days. ` get-item -path D:\User\Profiles\*\AppData\Roaming\Microsoft\Windows\Cookies\* -filter "*.txt" -force | Where-Object {$_.LastWriteTime -lt (get-date).addDays(-60) } ` But now I get a memory error. – Roy Jan 17 '19 at 10:43
0

This is a little bit simplified version. Just for the sake of safety I would run it first with -WhatIf to verify if the result is correct. Then just comment the other line

$path = ""
$pattern = ""
Get-ChildItem $path -recurse -directory -include $pattern |
   Remove-Item -WhatIf
   #Remove-Item -Force -ErrorAction SilentlyContinue
Bakudan
  • 19,134
  • 9
  • 53
  • 73
  • Hi Bakudan, that's for the quick reply. I had to change the code a bit because on of our security admins want it to remove the .txt files in the cookies folder that are older then 60 days. But now I get a memory error. `get-item -path D:\User\Profiles\*\AppData\Roaming\Microsoft\Windows\Cookies\* -filter "*.txt" -force | Where-Object {$_.LastWriteTime -lt (get-date).addDays(-60) }` – Roy Jan 17 '19 at 10:54
  • @Roy I haven't seen such error, so I can't help you with this. For the other thing, about the cookies older than 60 days - it will be better to update your question. – Bakudan Jan 18 '19 at 08:36