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 ?

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

6 Comments

@Roy Working with the pipeline (last example) is always preferable, when possible.
@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 Your code looks alright to me. Are there a lot cookie files to collect there?
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 stackoverflow.com/a/53400031/45375 for more.
Yeah I have around 700 users and every one of them has around 4000 or more cookies...
|
0

Try this one liner -

$path = "\\myserver\test\User\Profiles\"
Get-ChildItem $path -Recurse -Directory | Where-Object {$_.Name -eq 'Cookies'} | % {Remove-Item $_.FullName}

2 Comments

Let me try that, i'll keep you posted
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.
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

2 Comments

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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.