|
1 | 1 | function Invoke-WinUtilUninstallPSProfile { |
2 | | - <# |
3 | | - .SYNOPSIS |
4 | | - # Uninstalls the CTT PowerShell profile then restores the original profile. |
5 | | - #> |
6 | | - |
7 | | - Invoke-WPFRunspace -ArgumentList $PROFILE -DebugPreference $DebugPreference -ScriptBlock { |
8 | | - # Remap the automatic built-in $PROFILE variable to the parameter named $PSProfile. |
9 | | - param ($PSProfile) |
10 | | - |
11 | | - # Helper function used to uninstall a specific Nerd Fonts font package. |
12 | | - function Uninstall-NerdFonts { |
13 | | - # Define the parameters block for the Uninstall-NerdFonts function. |
14 | | - param ( |
15 | | - [string]$FontsPath = "$env:LOCALAPPDATA\Microsoft\Windows\Fonts", |
16 | | - [string]$FontFamilyName = "CaskaydiaCoveNerdFont" |
17 | | - ) |
18 | | - |
19 | | - # Get the list of installed fonts as specified by the FontFamilyName parameter. |
20 | | - $Fonts = Get-ChildItem $FontsPath -Recurse -Filter "*.ttf" | Where-Object { $_.Name -match $FontFamilyName } |
21 | | - |
22 | | - # Check if the specified fonts are currently installed on the system. |
23 | | - if ($Fonts) { |
24 | | - # Let the user know that the Nerd Fonts are currently being uninstalled. |
25 | | - Write-Host "===> Uninstalling: Nerd Fonts... <===" -ForegroundColor Yellow |
26 | | - |
27 | | - # Loop over the font files and remove each installed font file one-by-one. |
28 | | - $Fonts | ForEach-Object { |
29 | | - # Check if the font file exists on the disk before attempting to remove it. |
30 | | - if (Test-Path "$($_.FullName)") { |
31 | | - # Remove the found font files from the disk; uninstalling the font. |
32 | | - Remove-Item "$($_.FullName)" |
33 | | - } |
34 | | - } |
35 | | - } |
36 | | - |
37 | | - # Let the user know that the Nerd Fonts package has been uninstalled from the system. |
38 | | - if (-not $Fonts) { |
39 | | - Write-Host "===> Successfully Uninstalled: Nerd Fonts. <===" -ForegroundColor Yellow |
40 | | - } |
41 | | - |
42 | | - } |
43 | | - |
44 | | - # Helper function used to uninstall a specific Nerd Fonts font corresponding registry keys. |
45 | | - function Uninstall-NerdFontRegKeys { |
46 | | - # Define the parameters block for the Uninstall-NerdFontsRegKey function. |
47 | | - param ( |
48 | | - [string]$FontsRegPath = "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", |
49 | | - [string]$FontFamilyName = "CaskaydiaCove" |
50 | | - ) |
51 | | - |
52 | | - try { |
53 | | - # Get all properties (font registrations) from the registry path |
54 | | - $registryProperties = Get-ItemProperty -Path $FontsRegPath |
55 | | - |
56 | | - # Filter and remove properties that match the font family name |
57 | | - $registryProperties.PSObject.Properties | |
58 | | - Where-Object { $_.Name -match $FontFamilyName } | |
59 | | - ForEach-Object { |
60 | | - If ($_.Name -like "*$FontFamilyName*") { |
61 | | - Remove-ItemProperty -path $FontsRegPath -Name $_.Name -ErrorAction SilentlyContinue |
62 | | - } |
63 | | - } |
64 | | - } catch { |
65 | | - Write-Host "Error removing registry keys: $($_.exception.message)" -ForegroundColor Red |
66 | | - } |
67 | | - } |
68 | | - |
69 | | - # Check if Chris Titus Tech's PowerShell profile is currently available in the PowerShell profile folder. |
70 | | - if (Test-Path $PSProfile -PathType Leaf) { |
71 | | - # Set the GitHub repo path used for looking up the name of Chris Titus Tech's powershell-profile repo. |
72 | | - $GitHubRepoPath = "ChrisTitusTech/powershell-profile" |
73 | | - |
74 | | - # Get the unique identifier used to test for the presence of Chris Titus Tech's PowerShell profile. |
75 | | - $PSProfileIdentifier = (Invoke-RestMethod "https://api.github.com/repos/$GitHubRepoPath").full_name |
76 | | - |
77 | | - # Check if Chris Titus Tech's PowerShell profile is currently installed in the PowerShell profile folder. |
78 | | - if ((Get-Content $PSProfile) -match $PSProfileIdentifier) { |
79 | | - # Attempt to uninstall Chris Titus Tech's PowerShell profile from the PowerShell profile folder. |
80 | | - try { |
81 | | - # Get the content of the backup PowerShell profile and store it in-memory. |
82 | | - $PSProfileContent = Get-Content "$PSProfile.bak" |
83 | | - |
84 | | - # Store the flag used to check if OhMyPosh is in use by the backup PowerShell profile. |
85 | | - $OhMyPoshInUse = $PSProfileContent -match "oh-my-posh init" |
86 | | - |
87 | | - # Check if OhMyPosh is not currently in use by the backup PowerShell profile. |
88 | | - if (-not $OhMyPoshInUse) { |
89 | | - # If OhMyPosh is currently installed attempt to uninstall it from the system. |
90 | | - if (Get-Command oh-my-posh -ErrorAction SilentlyContinue) { |
91 | | - # Let the user know that OhMyPosh is currently being uninstalled from their system. |
92 | | - Write-Host "===> Uninstalling: OhMyPosh... <===" -ForegroundColor Yellow |
93 | | - |
94 | | - # Attempt to uninstall OhMyPosh from the system with the WinGet package manager. |
95 | | - winget uninstall -e --id JanDeDobbeleer.OhMyPosh |
96 | | - } |
97 | | - } else { |
98 | | - # Let the user know that the uninstallation of OhMyPosh has been skipped because it is in use. |
99 | | - Write-Host "===> Skipped Uninstall: OhMyPosh In-Use. <===" -ForegroundColor Yellow |
100 | | - } |
101 | | - } catch { |
102 | | - # Let the user know that an error was encountered when uninstalling OhMyPosh. |
103 | | - Write-Host "Failed to uninstall OhMyPosh. Error: $_" -ForegroundColor Red |
104 | | - } |
105 | | - |
106 | | - # Attempt to uninstall the specified Nerd Fonts package from the system. |
107 | | - try { |
108 | | - # Specify the directory that the specified font package will be uninstalled from. |
109 | | - [string]$FontsPath = "$env:LOCALAPPDATA\Microsoft\Windows\Fonts" |
110 | | - |
111 | | - # Specify the name of the font package that is to be uninstalled from the system. |
112 | | - [string]$FontFamilyName = "CaskaydiaCoveNerdFont" |
113 | | - |
114 | | - # Call the function used to uninstall the specified Nerd Fonts package from the system. |
115 | | - Uninstall-NerdFonts -FontsPath $FontsPath -FontFamilyName $FontFamilyName |
116 | | - |
117 | | - } catch { |
118 | | - # Let the user know that an error was encountered when uninstalling Nerd Fonts. |
119 | | - Write-Host "Failed to uninstall Nerd Fonts. Error: $_" -ForegroundColor Red |
120 | | - } |
121 | | - |
122 | | - # Attempt to uninstall the specified Nerd Fonts registry keys from the system. |
123 | | - try { |
124 | | - # Specify the registry path that the specified font registry keys will be uninstalled from. |
125 | | - [string]$FontsRegPath = "HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" |
126 | | - |
127 | | - # Specify the name of the font registry keys that is to be uninstalled from the system. |
128 | | - [string]$FontFamilyName = "CaskaydiaCove" |
129 | | - |
130 | | - # Call the function used to uninstall the specified Nerd Fonts registry keys from the system. |
131 | | - Uninstall-NerdFontRegKeys -FontsPath $FontsRegPath -FontFamilyName $FontFamilyName |
132 | | - |
133 | | - } catch { |
134 | | - # Let the user know that an error was encountered when uninstalling Nerd Font registry keys. |
135 | | - Write-Host "Failed to uninstall Nerd Font Registry Keys. Error: $_" -ForegroundColor Red |
136 | | - } |
137 | | - |
138 | | - # Attempt to uninstall the Terminal-Icons PowerShell module from the system. |
139 | | - try { |
140 | | - # Get the content of the backup PowerShell profile and store it in-memory. |
141 | | - $PSProfileContent = Get-Content "$PSProfile.bak" |
142 | | - |
143 | | - # Store the flag used to check if Terminal-Icons is in use by the backup PowerShell profile. |
144 | | - $TerminalIconsInUse = $PSProfileContent -match "Import-Module" -and $PSProfileContent -match "Terminal-Icons" |
145 | | - |
146 | | - # Check if Terminal-Icons is not currently in use by the backup PowerShell profile. |
147 | | - if (-not $TerminalIconsInUse) { |
148 | | - # If Terminal-Icons is currently installed attempt to uninstall it from the system. |
149 | | - if (Get-Module -ListAvailable Terminal-Icons) { |
150 | | - # Let the user know that Terminal-Icons is currently being uninstalled from their system. |
151 | | - Write-Host "===> Uninstalling: Terminal-Icons... <===" -ForegroundColor Yellow |
152 | | - |
153 | | - # Attempt to uninstall Terminal-Icons from the system with Uninstall-Module. |
154 | | - Uninstall-Module -Name Terminal-Icons |
155 | | - } |
156 | | - } else { |
157 | | - # Let the user know that the uninstallation of Terminal-Icons has been skipped because it is in use. |
158 | | - Write-Host "===> Skipped Uninstall: Terminal-Icons In-Use. <===" -ForegroundColor Yellow |
159 | | - } |
160 | | - } catch { |
161 | | - # Let the user know that an error was encountered when uninstalling Terminal-Icons. |
162 | | - Write-Host "Failed to uninstall Terminal-Icons. Error: $_" -ForegroundColor Red |
163 | | - } |
164 | | - |
165 | | - # Attempt to uninstall the Zoxide application from the system. |
166 | | - try { |
167 | | - # Get the content of the backup PowerShell profile and store it in-memory. |
168 | | - $PSProfileContent = Get-Content "$PSProfile.bak" |
169 | | - |
170 | | - # Store the flag used to check if Zoxide is in use by the backup PowerShell profile. |
171 | | - $ZoxideInUse = $PSProfileContent -match "zoxide init" |
172 | | - |
173 | | - # Check if Zoxide is not currently in use by the backup PowerShell profile. |
174 | | - if (-not $ZoxideInUse) { |
175 | | - # If Zoxide is currently installed attempt to uninstall it from the system. |
176 | | - if (Get-Command zoxide -ErrorAction SilentlyContinue) { |
177 | | - # Let the user know that Zoxide is currently being uninstalled from their system. |
178 | | - Write-Host "===> Uninstalling: Zoxide... <===" -ForegroundColor Yellow |
179 | | - |
180 | | - # Attempt to uninstall Zoxide from the system with the WinGet package manager. |
181 | | - winget uninstall -e --id ajeetdsouza.zoxide |
182 | | - } |
183 | | - } else { |
184 | | - # Let the user know that the uninstallation of Zoxide been skipped because it is in use. |
185 | | - Write-Host "===> Skipped Uninstall: Zoxide In-Use. <===" -ForegroundColor Yellow |
186 | | - } |
187 | | - } catch { |
188 | | - # Let the user know that an error was encountered when uninstalling Zoxide. |
189 | | - Write-Host "Failed to uninstall Zoxide. Error: $_" -ForegroundColor Red |
190 | | - } |
191 | | - |
192 | | - # Attempt to uninstall the CTT PowerShell profile from the system. |
193 | | - try { |
194 | | - # Try and remove the CTT PowerShell Profile file from the disk with Remove-Item. |
195 | | - Remove-Item $PSProfile |
196 | | - |
197 | | - # Let the user know that the CTT PowerShell profile has been uninstalled from the system. |
198 | | - Write-Host "Profile has been uninstalled. Please restart your shell to reflect the changes!" -ForegroundColor Magenta |
199 | | - } catch { |
200 | | - # Let the user know that an error was encountered when uninstalling the profile. |
201 | | - Write-Host "Failed to uninstall profile. Error: $_" -ForegroundColor Red |
202 | | - } |
203 | | - |
204 | | - # Attempt to move the user's original PowerShell profile backup back to its original location. |
205 | | - try { |
206 | | - # Check if the backup PowerShell profile exists before attempting to restore the backup. |
207 | | - if (Test-Path "$PSProfile.bak") { |
208 | | - # Restore the backup PowerShell profile and move it to its original location. |
209 | | - Move-Item "$PSProfile.bak" $PSProfile |
210 | | - |
211 | | - # Let the user know that their PowerShell profile backup has been successfully restored. |
212 | | - Write-Host "===> Restored Profile Backup. <===" -ForegroundColor Yellow |
213 | | - } |
214 | | - } catch { |
215 | | - # Let the user know that an error was encountered when restoring the profile backup. |
216 | | - Write-Host "Failed to restore profile backup. Error: $_" -ForegroundColor Red |
217 | | - } |
218 | | - |
219 | | - # Silently cleanup the oldprofile.ps1 file that was created when the CTT PowerShell profile was installed. |
220 | | - Remove-Item "$env:USERPROFILE\oldprofile.ps1" | Out-Null |
221 | | - } else { |
222 | | - # Let the user know that the CTT PowerShell profile is not installed and that the uninstallation was skipped. |
223 | | - Write-Host "===> Chris Titus Tech's PowerShell Profile Not Found. Skipped Uninstallation. <===" -ForegroundColor Magenta |
224 | | - } |
225 | | - } else { |
226 | | - # Let the user know that no PowerShell profile was found and that the uninstallation was skipped. |
227 | | - Write-Host "===> No PowerShell Profile Found. Skipped Uninstallation. <===" -ForegroundColor Magenta |
228 | | - } |
| 2 | + if (Test-Path ($Profile + '.bak')) { |
| 3 | + Remove-Item $Profile |
| 4 | + Rename-Item ($Profile + '.bak') -NewName $Profile |
| 5 | + } |
| 6 | + else { |
| 7 | + Remove-Item $Profile |
229 | 8 | } |
230 | | -} |
231 | 9 |
|
| 10 | + Write-Host "Successfully uninstalled CTT Powershell Profile" -ForegroundColor Green |
| 11 | +} |
0 commit comments