@@ -160,6 +160,62 @@ function Get-FileEncoding($Path) {
160160 }
161161}
162162
163+ <#
164+ . SYNOPSIS
165+ Removes posh-git from all PowerShell profile (startup) scripts.
166+ . DESCRIPTION
167+ Checks if your PowerShell profile scripts are importing posh-git
168+ and if so, removes lines related to posh-git from the script.
169+ . EXAMPLE
170+ PS C:\> Remove-PoshGitFromProfile
171+ Updates your profile script for all PowerShell hosts to not import the
172+ posh-git module when the current PowerShell host starts.
173+ . INPUTS
174+ None.
175+ . OUTPUTS
176+ None.
177+ #>
178+ function Remove-PoshGitFromProfile ([switch ]$WhatIf ) {
179+ $underTest = $false
180+
181+ $profilePath = $null
182+
183+ # Under test, we override some variables using $args as a backdoor.
184+ if ($args.Count -gt 0 ) {
185+ $profilePath = [string ]$args [0 ]
186+ $underTest = $true
187+ if ($args.Count -gt 1 ) {
188+ $ModuleBasePath = [string ]$args [1 ]
189+ }
190+ }
191+
192+ @ (
193+ $profilePath ,
194+ $PROFILE.CurrentUserCurrentHost ,
195+ $PROFILE.CurrentUserAllHosts ,
196+ $PROFILE.AllUsersCurrentHost ,
197+ $PROFILE.AllUsersAllHosts
198+ ) | Where-Object { $_ -and (Test-PoshGitImportedInScript $_ ) } |
199+ ForEach-Object {
200+ $path = $_
201+ $oldProfile = @ (Get-Content $path )
202+ $newProfile = New-Object System.Collections.Generic.List[string ]
203+
204+ switch - Wildcard ($oldProfile ) {
205+ ' *PoshGitPrompt*' { if ($WhatIf ) { Write-Host " What if: Removing line '$_ ' from '$path '." } continue ; }
206+ ' *Load posh-git example profile*' { if ($WhatIf ) { Write-Host " What if: Removing line '$_ ' from '$path '." } continue ; }
207+ ' *posh-git*profile.example.ps1*' { if ($WhatIf ) { Write-Host " What if: Removing line '$_ ' from '$path '." } continue ; }
208+ ' Import-Module *posh-git*' { if ($WhatIf ) { Write-Host " What if: Removing line '$_ ' from '$path '." } continue ; }
209+ default { $newProfile.Add ($_ ) }
210+ }
211+
212+ if ($newProfile.Count -lt $oldProfile.Length ) {
213+ $encoding = Get-FileEncoding $path
214+ Set-Content - Path $path - Value $newProfile - WhatIf:$WhatIf - Encoding $encoding
215+ }
216+ }
217+ }
218+
163219<#
164220. SYNOPSIS
165221 Gets a StringComparison enum value appropriate for comparing paths on the OS platform.
0 commit comments