Skip to content

Instantly share code, notes, and snippets.

@GarnetSunset
Last active April 13, 2024 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GarnetSunset/02236a759a0ada79662f56edccd58b4a to your computer and use it in GitHub Desktop.
Save GarnetSunset/02236a759a0ada79662f56edccd58b4a to your computer and use it in GitHub Desktop.
startup.ps1
# This part checks for internet connectivity before proceeding
function WaitForInternetConnection {
$isConnected = $false
while (-not $isConnected) {
try {
Write-Output "Trying to connect..."
Test-Connection -ComputerName "google.com" -Count 1 -ErrorAction Stop
$isConnected = $true
Write-Output "Connection successful."
} catch {
Write-Output "Failed to connect. Retrying in 5 seconds..."
Start-Sleep -Seconds 5
}
}
}
# Function to perform git pull and restart the script if it was updated
function UpdateScriptFromGit {
Write-Output "Checking for script updates..."
$gitOutput = git pull
Write-Output "Update output: $gitOutput"
if ($gitOutput -notlike "*Already up to date.*") {
Write-Output "Updates were applied, restarting the script..."
& $PSCommandPath
exit
}
}
# Log beginning of the script
Write-Output "Starting script..."
# Wait for an active internet connection
Write-Output "Waiting for an internet connection..."
WaitForInternetConnection
Write-Output "Internet connection established."
# Perform the update check
UpdateScriptFromGit
# Set the location to the script's root directory
Set-Location $PSScriptRoot
Write-Output "Set location to script's root directory."
# List of directories to remove
$pathsToRemove = @(
"$env:TEMP\*",
"C:\AMD",
"C:\Intel",
"C:\Nvidia",
"C:\Temp"
)
# Directories to skip when deleting the entire directory
$skipFullDelete = @(
"$env:TEMP",
"C:\Temp"
)
# Create an empty directory for the robocopy mirroring technique
$emptyDir = "C:\EmptyTempDir"
New-Item -Path $emptyDir -ItemType Directory -ErrorAction SilentlyContinue
foreach ($path in $pathsToRemove) {
Write-Output "Processing directory: $path..."
# Remove wildcard from path for robocopy
$adjustedPath = $path -replace '\\\*$', ''
if (Test-Path $adjustedPath) {
# Use robocopy to mirror the empty directory over the target path, effectively deleting its contents
robocopy $emptyDir $adjustedPath /MIR /W:1 /R:1
# Delete the directory itself if it's not in the skip list
if ($skipFullDelete -notcontains $adjustedPath) {
Remove-Item $adjustedPath -Force -Recurse -ErrorAction SilentlyContinue
}
}
}
# Clean up the temporary empty directory
Remove-Item $emptyDir -Force -Confirm:$false
Write-Output "Removal of specified directories completed."
# Remove registry key
Write-Output "Removing registry key..."
reg delete "HKEY_CURRENT_USER\SOFTWARE\SweetScape\010 Editor" /f
Write-Output "Registry key removed."
# Remove items from the Desktop
Write-Output "Removing items from the Desktop..."
Remove-Item "$env:USERPROFILE\Desktop\*" -Recurse -Force -ErrorAction SilentlyContinue -Confirm:$false
Write-Output "Items from the Desktop removed."
# Install and configure Spicetify
Write-Output "Installing and configuring Spicetify..."
spicetify backup -y | Out-Null
spicetify config current_theme sleek
spicetify config color_scheme cherry
spicetify upgrade -y | Out-Null
spicetify restore backup apply -y | Out-Null
spicetify apply -y | Out-Null
Write-Output "Spicetify configured."
# Shutdown WSL
Write-Output "Shutting down WSL..."
wsl --shutdown
Write-Output "WSL shutdown completed."
# Compact WSL2 Disk
Write-Output "Compacting WSL2 Disk..."
wslcompact -c -y
Write-Output "WSL2 Disk compacted."
# System scans and repairs
Write-Output "Starting system scans and repairs..."
sfc /scannow | Out-Null
DISM /Online /Cleanup-Image /RestoreHealth | Out-Null
Write-Output "System scans and repairs completed."
# Exit the script
Write-Output "Script completed."
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment