winutil/functions/microwin/Microwin-RemoveFileOrDirectory.ps1
CodingWonders 50071067b7
[MicroWin] April Update (#3317)
* Better formatting

A bit cleaner

* Remove unused scripts

This gets rid of file not found errors during setup

* Have a fallback method for Recall fixes

Go back to the delayed disablement procedure if we fail at modifying the manifest

* Ignore Recall disablement when disabled, and more

- If Recall is disabled, don't disable it again. This may be a waste of time
- Disable certain notification sources that I think are quite annoying, like those from Suggested or the Startup App Notification

* Hopefully? Get rid of News and Interests on Win10

Based on PR #3289, add News and Interests removal to MicroWin. Hopefully Windows 10 gets to cooperate this time, compared to last time, where it was quite stubborn.

*This means that people never give up trying to fight against something they don't like*

* Revert "Refactor preferChocolatey system to handle other package managers eas…" (#3323)

This reverts commit 89919494e5030f700664cfe01e9ffe3a49d48cd8.

* Fixed all the things that could possibly break

* Get rid of extra parameter

Package removal fallback fails here. Quite likely a copy from feature disablement

---------

Co-authored-by: Chris Titus <contact@christitus.com>
2025-05-05 10:24:12 -05:00

47 lines
2.0 KiB
PowerShell

function Microwin-RemoveFileOrDirectory([string]$pathToDelete, [string]$mask = "", [switch]$Directory = $false) {
if(([string]::IsNullOrEmpty($pathToDelete))) { return }
if (-not (Test-Path -Path "$($pathToDelete)")) { return }
$yesNo = Get-LocalizedYesNo
Write-Host "[INFO] In Your local takeown expects '$($yesNo[0])' as a Yes answer."
$itemsToDelete = [System.Collections.ArrayList]::new()
if ($mask -eq "") {
Write-Debug "Adding $($pathToDelete) to array."
[void]$itemsToDelete.Add($pathToDelete)
} else {
Write-Debug "Adding $($pathToDelete) to array and mask is $($mask)"
if ($Directory) {
$itemsToDelete = Get-ChildItem $pathToDelete -Include $mask -Recurse -Directory
} else {
$itemsToDelete = Get-ChildItem $pathToDelete -Include $mask -Recurse
}
}
foreach($itemToDelete in $itemsToDelete) {
$status = "Deleting $($itemToDelete)"
Write-Progress -Activity "Removing Items" -Status $status -PercentComplete ($counter++/$itemsToDelete.Count*100)
if (Test-Path -Path "$($itemToDelete)" -PathType Container) {
$status = "Deleting directory: $($itemToDelete)"
takeown /r /d $yesNo[0] /a /f "$($itemToDelete)"
icacls "$($itemToDelete)" /q /c /t /reset
icacls $itemToDelete /setowner "*S-1-5-32-544"
icacls $itemToDelete /grant "*S-1-5-32-544:(OI)(CI)F" /t /c /q
Remove-Item -Force -Recurse "$($itemToDelete)"
}
elseif (Test-Path -Path "$($itemToDelete)" -PathType Leaf) {
$status = "Deleting file: $($itemToDelete)"
takeown /a /f "$($itemToDelete)"
icacls "$($itemToDelete)" /q /c /t /reset
icacls "$($itemToDelete)" /setowner "*S-1-5-32-544"
icacls "$($itemToDelete)" /grant "*S-1-5-32-544:(OI)(CI)F" /t /c /q
Remove-Item -Force "$($itemToDelete)"
}
}
Write-Progress -Activity "Removing Items" -Status "Ready" -Completed
}