windows 卸载程序的批处理文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20861432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:13:26  来源:igfitidea点击:

Batch file to uninstall a program

windowsbatch-fileuninstall

提问by James

I'm trying to uninstall a program EXE via batch file and am not having any success.

我正在尝试通过批处理文件卸载程序 EXE,但没有任何成功。

The uninstall string found in the registry is as follows:

在注册表中找到的卸载字符串如下:

C:\PROGRA~1\Kofax\Capture\ACUnInst.exe /Workstation
C:\PROGRA~1\Kofax\Capture\UNWISE.EXE /U 
C:\PROGRA~1\Kofax\Capture\INSTALL.LOG

If I run that from CMD or batch it does nothing.

如果我从 CMD 或批处理运行它,它什么都不做。

If I run C:\PROGRA~1\Kofax\Capture\UNWISE.EXE /Ufrom CMD it will open up a dialog box to point to the INSTALL.LOG file and then proceed to uninstall.

如果我C:\PROGRA~1\Kofax\Capture\UNWISE.EXE /U从 CMD运行,它将打开一个对话框指向 INSTALL.LOG 文件,然后继续卸载。

At the end, it will ask me to click finish.

最后,它会要求我单击完成。

I need this to be silent, can you point me in the right direction? This is on XP and 7.

我需要保持沉默,你能指出我正确的方向吗?这是在 XP 和 7 上。

回答by Sunny

Every program that properly installs itself according to Microsoft's guidelines makes a registry entry in either HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall(for machine installs) or HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall(for user profile installs). Usually, the key for the program will be its GUID, or else the name of the program. Within that key will be an entry called UninstallString. This contains the command to execute to uninstall the program.

根据 Microsoft 的指南正确安装自身的每个程序都会在HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall(用于机器安装)或HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall(用于用户配置文件安装)中创建一个注册表项。通常,程序的键是它的 GUID,否则就是程序的名称。在该键中将有一个名为 的条目UninstallString。这包含要执行以卸载程序的命令。

If you already know ahead of time what you will be uninstalling, it should be easy enough to just put that in your batch file. It gets tricky when you try to automate that process though. You can use the reg command to get data from the registry, but it returns a lot of text around the actual value of a given key, making it hard to use. You may want to experiment with using VBscript or PowerShell, as they have better options for getting data from the registry into a variable.

如果您已经提前知道要卸载的内容,那么将其放入批处理文件中应该很容易。但是,当您尝试自动化该过程时,它会变得棘手。您可以使用 reg 命令从注册表中获取数据,但它会返回大量围绕给定键的实际值的文本,使其难以使用。您可能想尝试使用 VBscript 或 PowerShell,因为它们有更好的选择来将数据从注册表获取到变量中。

This might help you further.....

这可能会帮助你进一步......

How to Create a script via batch file that will uninstall a program if it was installed on windows 7 64-bit or 32-bit

如何通过批处理文件创建脚本,如果程序安装在 Windows 7 64 位或 32 位上,该脚本将卸载程序

回答by Rebecca

I've had the same problem and this is what I came up with. Before you start using this method though, you might wanna look up the name of the application on WMIC using CMD so..

我遇到了同样的问题,这就是我想出的。不过,在开始使用此方法之前,您可能想使用 CMD 在 WMIC 上查找应用程序的名称,因此..

First you wanna do: WMIC product > C:\Users\"currentuser"\Desktop\allapps.txt

首先你想做:WMIC product > C:\Users\"currentuser"\Desktop\allapps.txt

I'd recommend to output the command to an TXT file because it's really confusing to read it in the Cmd prompt, plus is easier to find the data you are looking for.

我建议将命令输出到 TXT 文件,因为在 Cmd 提示符下阅读它确实令人困惑,而且更容易找到您要查找的数据。

Now what you wanna do is find the actual name of the app... If you look at the code I put in, the app name says SkypeT because skype has "?" in the end of it and the command prompt can't interpretate that as it is.

现在你想要做的是找到应用程序的实际名称......如果你看看我输入的代码,应用程序名称会显示 SkypeT 因为 Skype 有“?” 最后,命令提示符无法按原样解释它。

After you got the app name, just put in the find in the 4th line and substitute, a few lines which contain my examples with skype...

获得应用程序名称后,只需将 find 放入第 4 行并替换,其中几行包含我的 Skype 示例...

Also you can probably creat a variable called %APP% and not worry as much, but at it's current it works just fine...

您也可以创建一个名为 %APP% 的变量而不必担心,但目前它工作得很好......

One thing to note! with me the msi /quiet command did not work, the program would not install or uninstall so I used /passive, which lets the users see what's going on.

一件事要注意!对我来说,msi /quiet 命令不起作用,程序无法安装或卸载,所以我使用了 /passive,它让用户看到发生了什么。

@Echo off
CD %cd%
:VerInstall
for /f "tokens=12,*" %%a in ('wmic product list system ^| Find /I "SkypeT"') do (
if Errorlevel = 0 ( 
Echo Skype is installed! )
if Errorlevel = 1 ( Echo Skype is not installed, proceding to the installation!
Ping localhost -n 7 >nul
goto :Reinstall )
)

:Status
tasklist /nh /fi "IMAGENAME eq "APP.exe" | find ":"> nul
if errorlevel = 1 goto :force
goto :Uninstall

:Force
echo We are killing the proccess... Please do not use the application during this process!
Ping localhost -n 7 > nul
taskkill /F /FI "STATUS eq RUNNING" /IM APP* /T
echo The task was killed with success! Uninstalling...
Ping localhost -n 7 > nul

:Uninstall
cls
for /f "tokens=12,*" %%a in ('wmic product list system ^| Find /I "SkypeT"') do (
set %%a=%%a: =%
msiexec.exe /x %%a /passive /norestart
)

:DoWhile
cls
Tasklist /fi "IMAGENAME eq msi*" /fi "STATUS eq RUNNING" | Find ":" >nul
if errorlevel = 1 (
echo Installation in progress
Goto :DoWhile
)
echo Skype is Uninstalled

:Reinstall
msiexec.exe /i SkypeSetup.msi /passive /norestart

:reinstallLoop

Tasklist /fi "IMAGENAME eq msi*" /fi "STATUS eq RUNNING" | Find ":" >nul
if errorlevel = 1 ( 
echo Installation in progress
goto :reinstallLoop
)
echo Skype is installed

:end
cls
color 0A
Echo Done!
exit

One last thing. I used this as an Invisible EXE task, so the user couldn't interact with the command prompt and eventually close the window (I know, I know, it makes the whole echoes stupid, but it was for testing purposes).for that I used BAT to EXE converter 2.3.1, you can put everything to work on the background and it will work very nicelly. if you want to show progress to users just write START Echo "info" and replace the info with whatever you want, it will open another prompt and show the info you need.

最后一件事。我将其用作 Invisible EXE 任务,因此用户无法与命令提示符交互并最终关闭窗口(我知道,我知道,这会使整个回声变得愚蠢,但这是出于测试目的)。为此,我使用 BAT 到 EXE 转换器 2.3.1,你可以把所有的东西都放在后台工作,它会很好地工作。如果您想向用户显示进度,只需编写 START Echo "info" 并将信息替换为您想要的任何信息,它将打开另一个提示并显示您需要的信息。

Remember, Wmic commands sometimes take up to 20 seconds to execute since it's querying the conputer's system, so it might look like it's doing nothing at first but it will run! ;) Good luck :)

请记住,Wmic 命令有时需要长达 20 秒的时间来执行,因为它正在查询计算机的系统,所以它可能看起来一开始什么都不做,但它会运行!;) 祝你好运 :)