#Requires -RunAsAdministrator | |
function Uninstall-Pester ([switch]$All) { | |
if ([IntPtr]::Size * 8 -ne 64) { throw "Run this script from 64bit PowerShell." } | |
#Requires -RunAsAdministrator | |
$pesterPaths = foreach ($programFiles in ($env:ProgramFiles, ${env:ProgramFiles(x86)})) { | |
$path = "$programFiles\WindowsPowerShell\Modules\Pester" | |
if ($null -ne $programFiles -and (Test-Path $path)) { | |
if ($All) { | |
Get-Item $path | |
} | |
else { | |
Get-ChildItem "$path\3.*" | |
} | |
} | |
} | |
if (-not $pesterPaths) { | |
"There are no Pester$(if (-not $all) {" 3"}) installations in Program Files and Program Files (x86) doing nothing." | |
return | |
} | |
foreach ($pesterPath in $pesterPaths) { | |
takeown /F $pesterPath /A /R | |
icacls $pesterPath /reset | |
# grant permissions to Administrators group, but use SID to do | |
# it because it is localized on non-us installations of Windows | |
icacls $pesterPath /grant "*S-1-5-32-544:F" /inheritance:d /T | |
Remove-Item -Path $pesterPath -Recurse -Force -Confirm:$false | |
} | |
} | |
Uninstall-Pester |
This comment has been minimized.
This comment has been minimized.
Might be worth having it run twice, one with the path included and one with the 32-bit path (I just ran into this myself): |
This comment has been minimized.
This comment has been minimized.
Thanks for the tip. This also won’t remove installations in your profile, and in everyone elses profiles. Would that be desirable behavior as well? For context, this was originally written just to get rid of the Pester that shipped with Windows and was “locked” by trusted installer. |
This comment has been minimized.
This comment has been minimized.
Note the path I mentioned was not in my profile. It seems like Pester 3.4.0 was installed 32-bit on my (windows 10) system so when I tried running your gist code, it was not catching it. When I ran the same code but added the " (x86)" into the path, it successfully removed the 32-bit copy of Pester. |
This comment has been minimized.
This comment has been minimized.
I did notice that. |
This comment has been minimized.
This comment has been minimized.
Updated to do that, will only work correctly when executed from x64 powershell, because of how env variables are defined, if anyone wants to fix that be my guest :) |
This comment has been minimized.
This comment has been minimized.
Gotcha! And I like it! Only question I have (might be because I'm on my phone right now) is where the $suffix variable comes from in the path(s)? |
This comment has been minimized.
This comment has been minimized.
Thanks for pointing it out, it should not be there. |
This comment has been minimized.
This comment has been minimized.
Will newer versions be shipped with new windows updates? |
This comment has been minimized.
This comment has been minimized.
Doubt that. |
This comment has been minimized.
This comment has been minimized.
Not sure if this makes it any 'cleaner' but here's my approach to finding any pre-existing path as we also were finding Pester in multiple folder locations especially if someone had also installed PowerShell v7: $modules = Get-Module -Name Pester -ListAvailable |
This comment has been minimized.
This comment has been minimized.
@clemmesserli wouldn't this also take any version in the users profile directory too? One thought to make the 64 bit/admin transition smoother, replace the top #Requires -RunAsAdministrator with this:
And delete the second #Requires -RunAsAdministrator Grabbed this from https://gist.github.com/talatham/ad406d5428ccec641f075a7019cd29a8 and added the -Verb parameter. |
This comment has been minimized.
This comment has been minimized.
@jcoryatjr Note that your version would fail on any 32-bit only systems. While it should be rare to nonexistent, it is a possibility to consider. |
This comment has been minimized.
This comment has been minimized.
@jasonrush good point. I spent some time and refactored into a true cmdlet using advanced function syntax. The module name is Utils and must be installed in a location located within $env:PSModulePath. The file structure is:
The Utils.psd1
Utils.ps1 file
Uninstall-Pester.ps1 file
|
This comment has been minimized.
Updated to use SID because Windows localizes name of Administrators group in non-english installations.