Replaced hardcoded paths with values from $env #118

This commit is contained in:
Raphire
2024-08-31 22:13:59 +02:00
parent 50e92337ad
commit 171c69a5e1

View File

@@ -397,16 +397,13 @@ function ForceRemoveEdge {
Write-Output "Removing leftover files..."
$appdata = $([Environment]::GetFolderPath('ApplicationData'))
$edgePaths = @(
"$env:ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk",
"$env:APPDATA\Microsoft\Internet Explorer\Quick Launch\Microsoft Edge.lnk",
"$env:APPDATA\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Microsoft Edge.lnk",
"$env:APPDATA\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Tombstones\Microsoft Edge.lnk",
"$env:PUBLIC\Desktop\Microsoft Edge.lnk",
"$env:USERPROFILE\Desktop\Microsoft Edge.lnk",
"$appdata\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Tombstones\Microsoft Edge.lnk",
"$appdata\Microsoft\Internet Explorer\Quick Launch\Microsoft Edge.lnk",
"$edgeStub"
)
@@ -475,7 +472,9 @@ function RegImport {
reg import "$PSScriptRoot\Regfiles\$path"
}
else {
reg load "HKU\Default" "C:\Users\Default\NTUSER.DAT" | Out-Null
$defaultUserPath = $env:USERPROFILE.Replace($env:USERNAME, 'Default\NTUSER.DAT')
reg load "HKU\Default" $defaultUserPath | Out-Null
reg import "$PSScriptRoot\Regfiles\Sysprep\$path"
reg unload "HKU\Default" | Out-Null
}
@@ -517,7 +516,8 @@ function ReplaceStartMenuForAllUsers {
}
# Get path to start menu file for all users
$usersStartMenuPaths = get-childitem -path "C:\Users\*\AppData\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState"
$userPathString = $env:USERPROFILE.Replace($env:USERNAME, "*\AppData\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState")
$usersStartMenuPaths = get-childitem -path $userPathString
# Go through all users and replace the start menu file
ForEach ($startMenuPath in $usersStartMenuPaths) {
@@ -525,16 +525,16 @@ function ReplaceStartMenuForAllUsers {
}
# Also replace the start menu file for the default user profile
$defaultProfile = "C:\Users\default\AppData\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState"
$defaultStartMenuPath = $env:USERPROFILE.Replace($env:USERNAME, 'Default\AppData\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState')
# Create folder if it doesn't exist
if (-not(Test-Path $defaultProfile)) {
new-item $defaultProfile -ItemType Directory -Force | Out-Null
Write-Output "Created LocalState folder for default user"
if (-not(Test-Path $defaultStartMenuPath)) {
new-item $defaultStartMenuPath -ItemType Directory -Force | Out-Null
Write-Output "Created LocalState folder for default user profile"
}
# Copy template to default profile
Copy-Item -Path $startMenuTemplate -Destination $defaultProfile -Force
Copy-Item -Path $startMenuTemplate -Destination $defaultStartMenuPath -Force
Write-Output "Replaced start menu for the default user profile"
Write-Output ""
}
@@ -544,7 +544,7 @@ function ReplaceStartMenuForAllUsers {
# Credit: https://lazyadmin.nl/win-11/customize-windows-11-start-menu-layout/
function ReplaceStartMenu {
param (
$startMenuBinFile = "C:\Users\$([Environment]::UserName)\AppData\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState\start2.bin",
$startMenuBinFile = "$env:LOCALAPPDATA\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState\start2.bin",
$startMenuTemplate = "$PSScriptRoot/Start/start2.bin"
)
@@ -698,9 +698,11 @@ else {
}
if ($global:Params.ContainsKey("Sysprep")) {
$defaultUserPath = $env:USERPROFILE.Replace($env:USERNAME, 'Default\NTUSER.DAT')
# Exit script if default user directory or NTUSER.DAT file cannot be found
if (-not (Test-Path "C:\Users\Default\NTUSER.DAT")) {
Write-Host "Error: Unable to start Win11Debloat in Sysprep mode, cannot find default user folder at 'C:\Users\Default\'" -ForegroundColor Red
if (-not (Test-Path "$defaultUserPath")) {
Write-Host "Error: Unable to start Win11Debloat in Sysprep mode, cannot find default user folder at '$defaultUserPath'" -ForegroundColor Red
AwaitKeyToExit
Exit
}
@@ -884,7 +886,7 @@ if ((-not $global:Params.Count) -or $RunDefaults -or $RunWin11Defaults -or ($SPP
Do {
Write-Host "Options:" -ForegroundColor Yellow
Write-Host " (n) Don't remove any pinned apps from the start menu" -ForegroundColor Yellow
Write-Host " (1) Remove all pinned apps from the start menu for this user only ($([Environment]::UserName))" -ForegroundColor Yellow
Write-Host " (1) Remove all pinned apps from the start menu for this user only ($env:USERNAME)" -ForegroundColor Yellow
Write-Host " (2) Remove all pinned apps from the start menu for all existing and new users" -ForegroundColor Yellow
$ClearStartInput = Read-Host "Remove all pinned apps from the start menu? (n/1/2)"
}
@@ -1238,7 +1240,7 @@ else {
continue
}
'ClearStart' {
Write-Output "> Removing all pinned apps from the start menu for user $([Environment]::UserName)..."
Write-Output "> Removing all pinned apps from the start menu for user $env:USERNAME..."
ReplaceStartMenu
Write-Output ""
continue