Skip to content

Commit

Permalink
Remake PR for Uninstall CTT PowerShell Profile (#3012)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryostrixx authored Nov 7, 2024
1 parent 7a0c404 commit 171441d
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 4 deletions.
16 changes: 13 additions & 3 deletions config/feature.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,23 @@
"ButtonWidth": "300",
"link": "https://christitustech.github.io/winutil/dev/features/Legacy-Windows-Panels/user"
},
"WPFWinUtilPSProfile": {
"WPFWinUtilInstallPSProfile": {
"Content": "Install CTT PowerShell Profile",
"category": "Powershell Profile",
"panel": "2",
"Order": "a083_",
"Type": "Button",
"ButtonWidth": "300"
"ButtonWidth": "300",
"link": "https://christitustech.github.io/winutil/dev/features/Powershell-Profile/PSProfileInstall"
},
"WPFWinUtilUninstallPSProfile": {
"Content": "Uninstall CTT PowerShell Profile",
"category": "Powershell Profile",
"panel": "2",
"Order": "a084_",
"Type": "Button",
"ButtonWidth": "300",
"link": "https://christitustech.github.io/winutil/dev/features/Powershell-Profile/PSProfileUninstall"
},
"WPFWinUtilSSHServer": {
"Content": "Enable OpenSSH Server",
Expand All @@ -322,4 +332,4 @@
"Type": "Button",
"ButtonWidth": "300"
}
}
}
38 changes: 38 additions & 0 deletions docs/dev/features/PowerShell-Profile/PSProfileInstall.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Install CTT PowerShell Profile

Last Updated: 2024-10-01


!!! info
The Development Documentation is auto generated for every compilation of WinUtil, meaning a part of it will always stay up-to-date. **Developers do have the ability to add custom content, which won't be updated automatically.**


<!-- BEGIN CUSTOM CONTENT -->

<!-- END CUSTOM CONTENT -->

<details>
<summary>Preview Code</summary>

```json
{
"Content": "Install CTT PowerShell Profile",
"category": "Powershell Profile",
"panel": "2",
"Order": "a083_",
"Type": "Button",
"ButtonWidth": "300",
"link": "https://christitustech.github.io/winutil/dev/features/Powershell-Profile/PSProfileInstall"
}
```

</details>


<!-- BEGIN SECOND CUSTOM CONTENT -->

<!-- END SECOND CUSTOM CONTENT -->


[View the JSON file](https://github.com/ChrisTitusTech/winutil/tree/main/config/feature.json)

38 changes: 38 additions & 0 deletions docs/dev/features/PowerShell-Profile/PSProfileUninstall.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Uninstall CTT PowerShell Profile

Last Updated: 2024-10-01


!!! info
The Development Documentation is auto generated for every compilation of WinUtil, meaning a part of it will always stay up-to-date. **Developers do have the ability to add custom content, which won't be updated automatically.**


<!-- BEGIN CUSTOM CONTENT -->

<!-- END CUSTOM CONTENT -->

<details>
<summary>Preview Code</summary>

```json
{
"Content": "Uninstall CTT PowerShell Profile",
"category": "Powershell Profile",
"panel": "2",
"Order": "a084_",
"Type": "Button",
"ButtonWidth": "300",
"link": "https://christitustech.github.io/winutil/dev/features/Powershell-Profile/PSProfileUninstall"
}
```

</details>


<!-- BEGIN SECOND CUSTOM CONTENT -->

<!-- END SECOND CUSTOM CONTENT -->


[View the JSON file](https://github.com/ChrisTitusTech/winutil/tree/main/config/feature.json)

103 changes: 103 additions & 0 deletions functions/private/Invoke-WinUtilInstallPSProfile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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
}
}
}
Loading

0 comments on commit 171441d

Please sign in to comment.