Get the App
SLTechnology News&Howtos  ›  Servers  › 

Create and run a Powershell script from SCCM to uninstall the software

Shulou Source: shulou.com Published: 2022-06-02 17:46:49 09月30日 Update

Recently encountered a demand: uninstall a small software for all computers, but this software is not deployed by SCCM, some are installed by users, some are packaged when the system is deployed, there are many versions, and the installation path is not the same!

Of course, the first thing that comes to mind is to use Powershell to do it. I tested it successively with Get-apppacke\ get-appxpacke, Get-WmiObject-Class win32_product and other methods, and finally solved it by looking for the UninstallString in the registry!

The methods are as follows:

1. Navigate to the registry location using PowerShell

X86 Script:

Set-Location HKLM:\ software\ microsoft\ Windows\ Currentversion\ uninstall

X64 Script:

Set-Location HKLM:\ software\ WOW6432Node\ microsoft\ Windows\ Currentversion\ uninstall

2. Query the name of the installed software in the registry Uninstall, such as Chrome. The Childitem in Uninstall is: Google Chrome, where Uninstallstring has the specific path of the uninstalled file, this file name and parameters:

"C:\ Program Files (x86)\ Google\ Chrome\ Application\ 74.0.3729.131\ Installer\ setup.exe"-uninstall-- system-level-- verbose-logging

This is the uninstall command we want to run. Some software only have execution files without parameters. For example, if Teamviewer has only one Uninstall.exe, we can use Uninstall.exe / S to silently uninstall it!

3. The complete PS script is as follows:

Set-Location

HKLM:\ software\ WOW6432Node\ microsoft\ Windows\ Currentversion\ uninstall

# specify the location of the registry

$UninstallTeamviewer = get-childitem-path * Teamviewer* | get-itemproperty |% {$_ .Uninstallstring}

# find the uninstall command and assign it to the variable: $UninstallTeamviewer

Start $UninstallTeamviewer / S

# run uninstall command in Powershell: $UninstallTeamviewer

For those who already have uninstall programs and parameters in the registry, there is no need to add the number of parameters: "/ S"

4. Create a script: right-click in SoftWare Library\ Overview\ Scripts and Create Script:

5. Paste the PowerShell script to the script. Next, finish!

TeamViewer

$OS = (Get-WmiObject Win32_OperatingSystem) .OSArchitecture

# check the operating system version

If ((gwmi win32_operatingsystem | select osarchitecture) .osarchitecture-eq "32-bit") {

# 32 Bit run the following steps

Set-Location HKLM:\ software\ microsoft\ Windows\ Currentversion\ uninstall

# specify the following registry location

$UninstallTeamviewer = get-childitem-path * Teamviewer* | get-itemproperty |% {$_ .Uninstallstring}

# find the uninstall command and assign it to the variable: $UninstallTeamviewer

Start $UninstallTeamviewer / S

# run uninstall command in Powershell: $UninstallTeamviewer

$UninstallTeamviewer

# output variable results to facilitate query and analysis

}

Else {

# 64 Bit run the following steps

Set-Location HKLM:\ software\ WOW6432Node\ microsoft\ Windows\ Currentversion\ uninstall

# specify the following registry location

$UninstallTeamviewer = get-childitem-path * Teamviewer* | get-itemproperty |% {$_ .Uninstallstring}

# find the uninstall command and assign it to the variable: $UninstallTeamviewer

Start $UninstallTeamviewer / S

# run uninstall command in Powershell: $UninstallTeamviewer

$UninstallTeamViewer

# output variable results to facilitate query and analysis

}

6. Approve the script: right-click on the script you just created and approve

In a production environment, user-created scripts need to be approved by a second person for security (that is, they cannot approve their own scripts). If you want to approve your own created scripts, please go to: Administration\ Site Configuration\ Sites\ Hierarchy Settings

Uncheck the box in front of "Script authors require additional script approver".

7. Run the script: right-click on the computer or set you want to run, and select: Run Script\ Next, that is, to start running.

8. You can also view the summary results of running scripts in Monitoring, and in the script status list, you can view the results of each script running on the client device. A script exit code of "0" usually indicates that the script has run successfully.

9. You can also inquire in the software report later!

10. If a software is installed using a MIS package, and the uninstall value will be "MsiExec.exe / I {23170F69-40C1-2701-1602-000001000000}", it is necessary to change to another PowerShell method, for example:

If ((gwmi win32_operatingsystem | select osarchitecture) .osarchitecture-eq "32-bit") {

# 32 Bit 7zip X32 1602 {23170F69-40C1-2701-1602-000001000000}

$script = {invoke-expression "msiexec / qn / x'{23170F69-40C1-2701-1602-00000000000}'"}

Invoke-Command-ScriptBlock $script

$script

} else {

# 64 Bit 7zip X64 1602 {23170F69-40C1-2702-1602-000001000000}

$script = {invoke-expression "msiexec / qn / x'{23170F69-40C1-2702-1602-00000000000}'"}

Invoke-Command-ScriptBlock $script

$script

}

Of course, if you don't have a SCCM environment, you can also try calling the above script in AD!

Tags: Scripts runs commands registries software variables locations results queries parameters files there are only methods methods steps versions environments users computers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information macOS OPPO Reno Linux Docker