-
Notifications
You must be signed in to change notification settings - Fork 0
/
删掉列出的包.ps1
68 lines (56 loc) · 2.09 KB
/
删掉列出的包.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 文本文件路径
$filePath = 'g:\pd.txt'
# 读取文件的每一行
$packageFullNames = Get-Content -Path $filePath
# 为将要删除的包创建两个空数组
$packagesToRemove = @()
$provisionedPackagesToRemove = @()
# 检查每个包全名
foreach ($pkgFullName in $packageFullNames) {
# 删除每一行两侧的空白字符
$pkgFullName = $pkgFullName.Trim()
# 判断trim后的字符串是否为空
if (![string]::IsNullOrWhiteSpace($pkgFullName)) {
# 检查是否已安装该包
$installedPackage = Get-AppxPackage -AllUsers | Where-Object { $_.PackageFullName -eq $pkgFullName }
if ($installedPackage -ne $null) {
# 如果已安装,添加到删除列表
$packagesToRemove += $installedPackage
}
# 检查是否预设了此包
$provisionedPackage = Get-AppxProvisionedPackage -Online | Where-Object { $_.PackageName -eq $pkgFullName }
if ($provisionedPackage -ne $null) {
# 如果预设了,添加到删除列表
$provisionedPackagesToRemove += $provisionedPackage
}
}
}
# 显示要删除的已安装包
Write-Host "The following installed packages will be removed:"
foreach ($pkg in $packagesToRemove) {
Write-Host $pkg.PackageFullName
}
# 显示将要删除的预设包
Write-Host "The following provisioned packages will be removed:"
foreach ($pkg in $provisionedPackagesToRemove) {
Write-Host $pkg.PackageName
}
# 提示用户确认
$confirm = Read-Host "Are you sure you want to remove these packages? (Y/N)"
# 验证选择
if ($confirm -eq 'Y') {
# 如果用户确认,删除已安装包
foreach ($pkg in $packagesToRemove) {
$pkg | Remove-AppxPackage -AllUsers
Write-Host "Removed installed package: $($pkg.PackageFullName)"
}
# 删除预设包
foreach ($pkg in $provisionedPackagesToRemove) {
$pkg | Remove-AppxProvisionedPackage -Online
Write-Host "Removed provisioned package: $($pkg.PackageName)"
}
Write-Host "All specified packages have been removed."
}
else {
Write-Host "Operation canceled."
}