贴代码:从命令行卸载程序, Windows XP/Vista/Win7

作者:半瓶墨水   链接:http://www.2maomao.com/blog/uninstall-from-command-line-for-windows/

主要写给自己用,因为天天都跟windows命令行打交道,装了个CCleaner就是因为它比windows自己的“添加/删除程序”要快。最近实现工作中要用到的东西,顺便做得更加通用一点,贴到这里。还挺方便的,因为命令行更快。

保存成u.py,然后如下运行:

D:\Documents\Dropbox\Coding\active>u apple
multiple matches, please choose:

  1  Apple Mobile Device Support
  2  Apple Software Update
  3  Apple Application Support

select a number, Ctrl+C to quit: 3

        Apple Mobile Device Support

press ENTER to uninstall, Ctrl+C to stop
Python语言: Command line uninstaller for Windows Applications
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#Author : Zhongfang Ren
#         yid: renzhongfang
#         email: realfun AT gmail.com
#         website: http://2maomao.com/blog/, http://fayaa.com
#Purpose: Command line uninstaller for windows XP(Vista and Win7 not tried yet)
#Date   : Wednesday, March 23, 2011, 10:42:34
#

import sys, os, getpass
import subprocess as exe
import _winreg as R

target = ''
if len(sys.argv) > 1:
  target = sys.argv[1]

uninstalls = []

for root in (R.HKEY_LOCAL_MACHINE, R.HKEY_CURRENT_USER):
  with R.OpenKey(root, "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall") as key:
    keys = []
    i = 0
    while True:
      try:
        keys.append(R.EnumKey(key, i))
        i += 1
      except Exception as e:
        #print e
        break

    for k in keys:
      try:
        with R.OpenKey(key, k) as hkey:
          try:
            v = R.QueryValueEx(hkey, "DisplayName")
            v = str(v[0])
            if v.lower().find(target.lower()) >= 0:
              uninstalls.append((v, R.QueryValueEx(hkey, "UninstallString")[0]))
          except Exception as e:
            #print e
            pass
      except:
        pass

try:
  if not uninstalls:
    print "uninstall for %s not found" % target
  elif len(uninstalls) == 1:
    print
    print "\t%s" % uninstalls[0][0]
    print
    raw_input("press ENTER to uninstall, Ctrl+C to stop")
    exe.call(uninstalls[0][1])
  else:
    print "multiple matches, please choose:\n"
    i = 1
    for nu in uninstalls:
      print "%3d " % i, nu[0]#, '\t', nu[1]
      i += 1
    print
    val = 0
    while val <=0 or val > len(uninstalls):
      try:
        val = int(raw_input("select a number, Ctrl+C to quit: "))
      except ValueError:
        pass
    name, uninst = uninstalls[val1]
    print
    print "\t%s" % name
    print
    raw_input("press ENTER to uninstall, Ctrl+C to stop")
    exe.call(uninst)
except KeyboardInterrupt:
  pass
finally:
  print

7 条评论 发表在“贴代码:从命令行卸载程序, Windows XP/Vista/Win7”上

  1. Breaker说道:

    不错,有时装的太多了,在列表中也半天找不见

  2. Izual_Yang说道:

    我用破解版的uninstall tool,可惜不免费……类似功能的免费软件有Revo Uninstaller(这货的pro版反而不好用)

  3. 半瓶墨水说道:

    @Izual_Yang 呵呵,所以还是自己动手丰衣足食啊

  4. littlewater说道:

    你最近命令行用得很顺手嘛=w=

  5. G字头老大说道:

    简明的绿色代码总是高效的!!

  6. Allen说道:

    假如用u.py去uninstall python会怎么样?

  7. bombless说道:

    貌似除了SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall之外还有2个目录有安装文件。。的信息。。

    以前删游戏的时候也弄过这些。。C写的

    即使注册表是NT的核心设施,感觉也不需要用的这么过吧。。

    很多时候把配置放在安装目录或者我的文档不就行了。。哎。。感觉现在的情况挺混乱的

留下回复