Could I get some feedback on my PowerShell script for clearing Edge browser data?

0
5
Asked By CuriousCactus123 On

I have this PowerShell script called `Clear-Edge-Cache.ps1` that's supposed to help fix issues with the Edge browser by clearing various types of its data without affecting cookies or saved passwords. The script includes an Incident Response mode that backs up user data for malware analysis. I'd love for someone to review the script before I start testing it. If you primarily use other browsers and could give it a run, that would be even better. I apologize for the state of the script; it's a work in progress, and that's part of why I'm asking for help. Thanks in advance!

3 Answers

Answered By CodeReviewPal On

Just a few things to consider:

1. That large text block at the top? Convert it into proper help documentation so users can use `get-help` directly.
2. Your switches `-moderate` and `-aggressive` need some clarity on their differences.
3. Merge your three logging functions into one with a `-severity` switch to make it cleaner
4. Variable names like `$d` and `$f` aren't very descriptive—renaming them helps everyone understand the code better.
5. The Read-Host prompts come abruptly; consider using the `-Confirm` parameter instead.

Answered By TechGuru42 On

You've got some try/catch blocks that could be tweaked. For example:

```powershell
if ($edgeWasRunning) {
Write-Info "Closing Edge processes..."
try { Get-Process msedge -ErrorAction Stop | Stop-Process -Force -ErrorAction Stop } catch {}
}
```

Instead of using Stop-Process, which is like a SIGKILL, consider using `(Get-Process msedge).CloseMainWindow()` for a more graceful shutdown. Also, there are some parts from lines 206 to 264 that look a bit convoluted; rewriting the conditions could make it cleaner. Just a heads-up on the ShouldProcess attribute too—it's in your script, but it doesn't seem to be implemented anywhere!

CuriousCactus123 -

Thank you for your insights!

DebugMaster -

The section you mentioned deals with restoring data after running the Incident Response mode, but I'm not very confident it functions correctly. I'm learning, and this script was meant to be a handy tool for various cleanup scenarios, mostly to help maintain browser performance. We will see how it goes!

Answered By ScriptSavant On

I'm reviewing it by step but not executing it wholly yet. Here’s a suggestion for handling Edge profiles:

```powershell
$profiles = Get-ChildItem -LiteralPath $edgeRoot -Directory -ErrorAction SilentlyContinue |
Where-Object { $_.Name -ne 'System Profile' -and (Test-Path (Join-Path $_.FullName 'Preferences')) }
```

Since default profiles are included in your wildcard search, simplifying it could save you some hassle. Try using direct assignment rather than adding elements to an array when you build paths—less code to maintain! Enjoy your meal while you’re at it!

CuriousCactus123 -

Thanks a lot! This is super helpful, and I appreciate the time you’re taking to look through it!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.