Using Powershell script to Uninstall old version and install new version 7 zip

Kumar 1 Reputation point
2022-11-28T12:28:25.677+00:00

Hello

I'm very much new to the scripting world. I don't know basics of scripting. I have an assignment on my course, where i have been provided with a script uninstall old version and to install 7-zip on my test computer using powershell script

@Andreas Baumgarten can you please help with this

Get-ChildItem -Path "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
-Recurse | Where {$.GetValue("DisplayName") -like "*7*zip*" }
| ForEach-Object {
$
.GetValue("DisplayName"); $_.GetValue("UninstallString") ;
; Start-Process $uninstall;
}

$url = "https://d3.7-zip.org/a/7z2201-x64.exe"
$filePath = "C:\Junk\7z1900-×64.exe"
$ProgressPreference = 0
Invoke-WebRequest $url -OutFile $filePath
& $filePath /S

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,460 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. DaveK 1,851 Reputation points
    2022-11-28T16:19:45.437+00:00

    Hi,

    Its typical for the "uninstallstring" to actually be an installation string so if you wish to script it silently the string needs to be modified slightly. This should get you started on removal:

    Get-ChildItem -Path "HKLM:SOFTWARE\wow6432node\Microsoft\Windows\CurrentVersion\Uninstall","HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" -Recurse |   
    Where {$_.GetValue("DisplayName") -like "*zip*"} |   
    ForEach-Object {  
    	$dn = $_.GetValue("DisplayName")  
    	$uninstallGUID = "{" + $_.GetValue("UninstallString").Split("{")[-1]  
    	Start-Process -FilePath "msiexec.exe" -ArgumentList  "/X", $uninstallGUID, "/qb"  
    }  
    

    Line1 list both uninstall locations so you should pickup 32 and 64bit versions.
    Line 5 added a leading curly bracket, then splits the uninstallstring at the first curly bracket and appends the last part of the split leaving you with a complete GUID
    Line 6 uses start process to run msiexec.exe passing arguments to uninstall (/X), the GUID of what you wish to uninstall and finally quiet mode to make it not prompt. For troubleshooting and initial runs I always use /qb as that is quiet with basic UI so you can see it actually pops up and runs. When your happy you can change that to /qn which is quiet with no UI.

    As its an assignment I'm not going to post a complete solution :)

    0 comments No comments