winutil/functions/private/Invoke-WinUtilInstallPSProfile.ps1
2024-11-05 13:04:26 -08:00

103 lines
6.0 KiB
PowerShell

function Invoke-WinUtilInstallPSProfile {
<#
.SYNOPSIS
Backs up your original profile then installs and applies the CTT PowerShell profile.
#>
Invoke-WPFRunspace -ArgumentList $PROFILE -DebugPreference $DebugPreference -ScriptBlock {
# Remap the automatic built-in $PROFILE variable to the parameter named $PSProfile.
param ($PSProfile)
function Invoke-PSSetup {
# Define the URL used to download Chris Titus Tech's PowerShell profile.
$url = "https://raw.githubusercontent.com/ChrisTitusTech/powershell-profile/main/Microsoft.PowerShell_profile.ps1"
# Get the file hash for the user's current PowerShell profile.
$OldHash = Get-FileHash $PSProfile -ErrorAction SilentlyContinue
# Download Chris Titus Tech's PowerShell profile to the 'TEMP' folder.
Invoke-RestMethod $url -OutFile "$env:TEMP/Microsoft.PowerShell_profile.ps1"
# Get the file hash for Chris Titus Tech's PowerShell profile.
$NewHash = Get-FileHash "$env:TEMP/Microsoft.PowerShell_profile.ps1"
# Store the file hash of Chris Titus Tech's PowerShell profile.
if (!(Test-Path "$PSProfile.hash")) {
$NewHash.Hash | Out-File "$PSProfile.hash"
}
# Check if the new profile's hash doesn't match the old profile's hash.
if ($NewHash.Hash -ne $OldHash.Hash) {
# Check if oldprofile.ps1 exists and use it as a profile backup source.
if (Test-Path "$env:USERPROFILE\oldprofile.ps1") {
Write-Host "===> Backup File Exists... <===" -ForegroundColor Yellow
Write-Host "===> Moving Backup File... <===" -ForegroundColor Yellow
Copy-Item "$env:USERPROFILE\oldprofile.ps1" "$PSProfile.bak"
Write-Host "===> Profile Backup: Done. <===" -ForegroundColor Yellow
} else {
# If oldprofile.ps1 does not exist use $PSProfile as a profile backup source.
# Check if the profile backup file has not already been created on the disk.
if ((Test-Path $PSProfile) -and (-not (Test-Path "$PSProfile.bak"))) {
# Let the user know their PowerShell profile is being backed up.
Write-Host "===> Backing Up Profile... <===" -ForegroundColor Yellow
# Copy the user's current PowerShell profile to the backup file path.
Copy-Item -Path $PSProfile -Destination "$PSProfile.bak"
# Let the user know the profile backup has been completed successfully.
Write-Host "===> Profile Backup: Done. <===" -ForegroundColor Yellow
}
}
# Let the user know Chris Titus Tech's PowerShell profile is being installed.
Write-Host "===> Installing Profile... <===" -ForegroundColor Yellow
# Start a new hidden PowerShell instance because setup.ps1 does not work in runspaces.
Start-Process -FilePath "pwsh" -ArgumentList "-ExecutionPolicy Bypass -NoProfile -Command `"Invoke-Expression (Invoke-WebRequest `'https://github.com/ChrisTitusTech/powershell-profile/raw/main/setup.ps1`')`"" -WindowStyle Hidden -Wait
# Let the user know Chris Titus Tech's PowerShell profile has been installed successfully.
Write-Host "Profile has been installed. Please restart your shell to reflect the changes!" -ForegroundColor Magenta
# Let the user know Chris Titus Tech's PowerShell profile has been setup successfully.
Write-Host "===> Finished Profile Setup <===" -ForegroundColor Yellow
} else {
# Let the user know Chris Titus Tech's PowerShell profile is already fully up-to-date.
Write-Host "Profile is up to date" -ForegroundColor Magenta
}
}
# Check if PowerShell Core is currently installed as a program and is available as a command.
if (Get-Command "pwsh" -ErrorAction SilentlyContinue) {
# Check if the version of PowerShell Core currently in use is version 7 or higher.
if ($PSVersionTable.PSVersion.Major -ge 7) {
# Invoke the PowerShell Profile setup script to install Chris Titus Tech's PowerShell Profile.
Invoke-PSSetup
} else {
# Let the user know that PowerShell 7 is installed but is not currently in use.
Write-Host "This profile requires Powershell 7, which is currently installed but not used!" -ForegroundColor Red
# Load the necessary .NET library required to use Windows Forms to show dialog boxes.
Add-Type -AssemblyName System.Windows.Forms
# Display the message box asking if the user wants to install PowerShell 7 or not.
$question = [System.Windows.Forms.MessageBox]::Show(
"Profile requires Powershell 7, which is currently installed but not used! Do you want to install the profile for Powershell 7?",
"Question",
[System.Windows.Forms.MessageBoxButtons]::YesNo,
[System.Windows.Forms.MessageBoxIcon]::Question
)
# Proceed with the installation and setup of the profile as the user pressed the 'Yes' button.
if ($question -eq [System.Windows.Forms.DialogResult]::Yes) {
Invoke-PSSetup
} else {
# Let the user know the setup of the profile will not proceed as they pressed the 'No' button.
Write-Host "Not proceeding with the profile setup!" -ForegroundColor Magenta
}
}
} else {
# Let the user know that the profile requires PowerShell Core but it is not currently installed.
Write-Host "This profile requires Powershell Core, which is currently not installed!" -ForegroundColor Red
}
}
}