## MSI與EXE 安裝的軟體有分MSI版跟EXE版,所以要撈兩次 ```powershell= # 列出所有 MSI 安裝的軟體 # MSI方法一: Get-CimInstance -Class Win32_Product | Where-Object Name -Like 'KeePassXC' | Select-Object Name, Version, IdentifyingNumber # MSI方法二: Get-WmiObject -Query "SELECT * FROM Win32_Product WHERE Name Like 'KeePassXC'" | Select-Object Name, Version, IdentifyingNumber # 列出所有 EXE 安裝的軟體 $uninstallKey = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall", "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" -ErrorAction SilentlyContinue | Get-ItemProperty | Where-Object { $_.DisplayName -and $_.UninstallString } $uninstallKey | Select-Object DisplayName, DisplayVersion ``` ### Get-CIMInstance Vs Get-WMIObject https://www.twblogs.net/a/61529fc65f3d1b1b738037f8 https://www.progress.com/blogs/get-ciminstance-vs-get-wmiobject-whats-the-difference Common Information Model (CIM) 是開源的標準,提供systems, networks, applications and services管理信息的定義,由 Distributed Management Task Force (DMTF)開發和維護。 Windows Management Interface (WMI) 是微軟針對windows平臺,對CIM的一種實現。但是微軟進加入了DCOM和RPC。WMI cmdlets使用DCOM訪問遠程機器,但DCOM這玩意屬於“防火牆不友好”,就是要使用很多端口,你得配置好多防火牆規則,遠程機器必須允許135, 445, 1024-1034端口,不方便不說還不安全。 WMI 正在被棄用。如今,PowerShell嚴重依賴PSRemoting及其安全功能。Microsoft推出了基於 CIM 的 cmdlet 和 PowerShell v3.0 的 PSRemoting。任何運行 Windows 7 或更高版本和 Server 2008 SP2 及更高版本的電腦都可以並且應該使用 PSRemoting 而不是 WMI。 ## 找出特定軟體並進行移除 以7-zip為例 ```powershell= $target = '*7-zip*' # 針對MSI版本 Write-Output "嘗試MSI移除" $query = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -like $target } if ($query) { foreach ($install in $query) { $installName = $install.Name Start-Process -FilePath msiexec.exe -ArgumentList "/x $($install.IdentifyingNumber) /quiet /qn /norestart" -Wait Write-Output "執行移除$($installName)完畢!!" } } else { Write-Output "找不到MSI安裝" } # 針對EXE版本 Write-Output "嘗試EXE移除" $query = (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall", "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" -ErrorAction SilentlyContinue | Get-ItemProperty | Where-Object { $_.DisplayName -Like $target }) if ($query) { $query | ForEach-Object { $command = '' $installName = $_.Name if($_.UninstallString) { $command = $_.UninstallString } if($query.QuietUninstallString){ $command = $_.QuietUninstallString } if($command){ cmd.exe /c $command Write-Output "執行移除$($installName)完畢!!" } else { Write-Output "找不到EXE安裝" } } } else { Write-Output "找不到EXE安裝" } ``` ## 找出特定軟體並進行更新