TwinCAT / Beckhoff 完整移除 PowerShell 腳本
Problem
TwinCAT 安裝後會在系統中留下大量 Beckhoff 元件(XAE、XAR、TC3 Functions、驅動程式等),透過控制台逐一卸載非常耗時,且容易遺漏。需要乾淨移除時(例如版本衝突、重裝前清理),手動操作不切實際。
Environment
- Windows 10 / 11
- PowerShell 5.1+(系統內建)
- 需要系統管理員權限執行
Solution
以系統管理員身份開啟 PowerShell,執行以下腳本:
$RegKeys = @(
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\'
'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\'
)
$Apps = $RegKeys |
Get-ChildItem |
Get-ItemProperty |
Where-Object { $_.DisplayName -like 'Beckhoff*' -and $_.UninstallString }
# 先列出會被卸載的項目,確認一下
$Apps | Select-Object DisplayName, UninstallString
# 確認沒問題後再跑下面這段
foreach ($App in $Apps) {
$UninstallString = if ($App.UninstallString -match '^msiexec') {
"$($App.UninstallString -replace '/I', '/X') /qn /norestart"
} else {
$App.UninstallString
}
Write-Host "Uninstalling: $($App.DisplayName)" -ForegroundColor Yellow
Start-Process -FilePath cmd -ArgumentList '/c', $UninstallString -NoNewWindow -Wait
Write-Host "Done: $($App.DisplayName)" -ForegroundColor Green
}
運作原理
- 掃描 Registry 的
Uninstall鍵(同時涵蓋 64-bit 和 32-bit 路徑) - 篩選
DisplayName以Beckhoff開頭且有UninstallString的項目 - 第一段先列出清單供確認,避免誤刪
- 第二段逐一執行卸載:MSI 安裝的元件自動轉換為靜默卸載(
/X ... /qn /norestart),非 MSI 的直接執行原始卸載指令
注意事項
- 執行前先跑清單確認(只執行到
Select-Object那行),確保沒有抓到不該卸載的東西 - 卸載過程可能需要數分鐘,取決於安裝的元件數量
- 完成後建議重啟電腦,讓驅動程式和服務的移除生效
- 如果有 TwinCAT 服務正在執行,建議先停止
TcSysSrv服務再卸載