7
7
Utilizes LGPO.exe to apply group policy item where neceassary.
8
8
Utilizes MDT/SCCM TaskSequence property control
9
9
Configurable using custom variables in MDT/SCCM
10
+
11
+ . EXAMPLE
12
+ powershell.exe -ExecutionPolicy Bypass -file "AppOptimizeAndConfig.ps1"
13
+
10
14
.INFO
15
+ Script: AppOptimizeAndConfig.ps1
11
16
Author: Richard Tracy
12
17
Email: richard.tracy@hotmail.com
13
18
Twitter: @rick2_1979
@@ -237,7 +242,7 @@ Function Write-LogEntry{
237
242
Out-File - InputObject $LogFormat - Append - NoClobber - Encoding Default - FilePath $OutputLogFile - ErrorAction Stop
238
243
}
239
244
catch {
240
- Write-Host (" [{0}] [{1}] :: Unable to append log entry to [{1}], error: {2}" -f $LogTimePlusBias , $ScriptSource , $OutputLogFile , $_.Exception.ErrorMessage ) - ForegroundColor Red
245
+ Write-Host (" [{0}] [{1}] :: Unable to append log entry to [{1}], error: {2}" -f $LogTimePlusBias , $ScriptSource , $OutputLogFile , $_.Exception.Message ) - ForegroundColor Red
241
246
}
242
247
}
243
248
End {
@@ -516,17 +521,20 @@ Function Set-SystemSetting {
516
521
# verify the registry value has been set
517
522
Try {
518
523
If ( -not (Test-Path ($RegHive + ' \' + $RegKeyPath )) ){
519
- Write-LogEntry (" Key was not set; Hardcoding registry keys [{0}\{1}] with value [{2}]" -f ($RegHive + ' \' + $RegKeyPath ), $RegKeyName , $Value ) - Severity 0 - Source ${CmdletName}
520
- New-Item - Path ($RegHive + ' \' + $RegKeyPath ) - Force - WhatIf:$WhatIfPreference - ErrorAction SilentlyContinue | Out-Null
521
- New-ItemProperty - Path ($RegHive + ' \' + $RegKeyPath ) - Name $RegKeyName - PropertyType $Type - Value $Value - Force:$Force - WhatIf:$WhatIfPreference - ErrorAction SilentlyContinue - PassThru
524
+ Write-LogEntry (" Path was not found; Creating path and setting registry keys [{0}\{1}] with value [{2}]" -f ($RegHive + ' \' + $RegKeyPath ), $RegKeyName , $Value ) - Severity 0 - Source ${CmdletName}
525
+ # New-Item -Path ($RegHive +'\'+ $RegKeyPath) -Force -WhatIf:$WhatIfPreference -ErrorAction Stop | Out-Null
526
+ New-Item ($RegHive + ' \' + $RegKeyPath ) - Force:$Force - WhatIf:$WhatIfPreference - ErrorAction Stop | New-ItemProperty - Name $RegKeyName - PropertyType $Type - Value $Value - Force:$Force - ErrorAction Stop | Out-Null
527
+ # wait for registry path to popluate (only on slower systems)
528
+ # start-sleep 2
529
+ # New-ItemProperty -Path ($RegHive +'\'+ $RegKeyPath) -Name $RegKeyName -PropertyType $Type -Value $Value -Force:$Force -WhatIf:$WhatIfPreference -ErrorAction Stop | Out-Null
522
530
}
523
531
Else {
524
- Write-LogEntry (" Key name not found. Creating key name [{1}] at path [{0}] with value [{2}]" -f ($RegHive + ' \' + $RegKeyPath ), $RegKeyName , $Value ) - Source ${CmdletName}
525
- Set-ItemProperty - Path ($RegHive + ' \' + $RegKeyPath ) - Name $RegKeyName - Value $Value - Force:$Force - WhatIf:$WhatIfPreference - ErrorAction SilentlyContinue - PassThru
532
+ Write-LogEntry (" Setting key name [{1}] at path [{0}] with value [{2}]" -f ($RegHive + ' \' + $RegKeyPath ), $RegKeyName , $Value ) - Source ${CmdletName}
533
+ Set-ItemProperty - Path ($RegHive + ' \' + $RegKeyPath ) - Name $RegKeyName - Value $Value - Force:$Force - WhatIf:$WhatIfPreference - ErrorAction Stop | Out-Null
526
534
}
527
535
}
528
536
Catch {
529
- Write-LogEntry (" Unable to set registry key [{0}\{1}\{2}] with value [{3}] " -f $RegHive , $RegKeyPath , $RegKeyName , $Value ) - Severity 2 - Source ${CmdletName}
537
+ Write-LogEntry (" Unable to configure registry key [{0}\{1}\{2}]. {4} " -f $RegHive , $RegKeyPath , $RegKeyName , $Value , $_ .Exception.Message ) - Severity 3 - Source ${CmdletName}
530
538
}
531
539
532
540
}
@@ -701,7 +709,12 @@ Function Set-UserSetting {
701
709
If ($HiveLoaded -eq $true ) {
702
710
If ($Message ){Write-LogEntry (" {0} for User [{1}].." -f $Message , $UserName )}
703
711
If ($Remove ){
704
- Remove-ItemProperty " $RegHive \$ ( $UserProfile.SID ) \$RegKeyPath " - Name $RegKeyName - Force:$Force - WhatIf:$WhatIfPreference - ErrorAction SilentlyContinue | Out-Null
712
+ Try {
713
+ Remove-ItemProperty " $RegHive \$ ( $UserProfile.SID ) \$RegKeyPath " - Name $RegKeyName - Force:$Force - WhatIf:$WhatIfPreference - ErrorAction SilentlyContinue | Out-Null
714
+ }
715
+ Catch {
716
+ Write-LogEntry (" Unable to remove registry key [{0}\{1}\{2}]. {4}" -f $RegHive , $RegKeyPath , $RegKeyName , $Value , $_.Exception.Message ) - Severity 3 - Source ${CmdletName}
717
+ }
705
718
}
706
719
Else {
707
720
Set-SystemSetting - Path " $RegHive \$ ( $UserProfile.SID ) \$RegKeyPath " - Name $RegKeyName - Type $Type - Value $Value - Force:$Force - WhatIf:$WhatIfPreference - TryLGPO:$TryLGPO
@@ -720,7 +733,12 @@ Function Set-UserSetting {
720
733
Else {
721
734
If ($Message ){Write-LogEntry (" {0} for [{1}].." -f $Message , $ProfileList.UserName )}
722
735
If ($Remove ){
723
- Remove-ItemProperty " $RegHive \$RegKeyPath \$RegKeyPath " - Name $RegKeyName - Force:$Force - WhatIf:$WhatIfPreference - ErrorAction SilentlyContinue | Out-Null
736
+ Try {
737
+ Remove-ItemProperty " $RegHive \$RegKeyPath \$RegKeyPath " - Name $RegKeyName - Force:$Force - WhatIf:$WhatIfPreference - ErrorAction SilentlyContinue | Out-Null
738
+ }
739
+ Catch {
740
+ Write-LogEntry (" Unable to remove registry key [{0}\{1}\{2}]. {4}" -f $RegHive , $RegKeyPath , $RegKeyName , $Value , $_.Exception.Message ) - Severity 3 - Source ${CmdletName}
741
+ }
724
742
}
725
743
Else {
726
744
Set-SystemSetting - Path " $RegHive \$RegKeyPath " - Name $RegKeyName - Type $Type - Value $Value - Force:$Force - WhatIf:$WhatIfPreference - TryLGPO:$TryLGPO
@@ -1104,6 +1122,7 @@ If($RemoveAppxPackages)
1104
1122
1105
1123
$p = 1
1106
1124
$c = 0
1125
+ $d = 0
1107
1126
# Loop through the list of appx packages
1108
1127
foreach ($App in $AppArrayList ) {
1109
1128
@@ -1113,49 +1132,57 @@ If($RemoveAppxPackages)
1113
1132
}
1114
1133
else {
1115
1134
# Gather package names
1116
- $AppPackageFullName = Get-AppxPackage - Name $App.Name | Select-Object - ExpandProperty PackageFullName
1135
+ $AppPackageDetails = Get-AppxPackage - AllUsers - Name $App.Name
1117
1136
1118
- $AppProvisioningPackageName = Get-AppxProvisionedPackage - Online | Where-Object { $_.DisplayName -like $App .Name } | Select-Object - ExpandProperty PackageName
1137
+ $AppProvisioningPackageName = Get-AppxProvisionedPackage - Online | Where-Object { $_.DisplayName -like $AppPackageDetails .Name } | Select-Object - ExpandProperty PackageName
1119
1138
1120
1139
# Attempt to remove AppxPackage
1121
- if ($null -ne $AppPackageFullName ) {
1122
- Show-ProgressStatus - Message (" Removing application package: {0}" -f $App .Name ) - Step $p - MaxStep $AppArrayList.count
1140
+ if ($null -ne $AppPackageDetails ) {
1141
+ Show-ProgressStatus - Message (" Removing application package: {0}" -f $AppPackageDetails .Name ) - Step $p - MaxStep $AppArrayList.count
1123
1142
1124
1143
try {
1125
- Remove-AppxPackage - Package $AppPackageFullName - ErrorAction Stop | Out-Null
1144
+ Remove-AppxPackage - AllUsers - Package $AppPackageDetails .PackageFullName - ErrorAction Stop | Out-Null
1126
1145
1127
- Write-LogEntry - Message (" Successfully removed application package: {0}" -f $App .Name ) - Outhost
1146
+ Write-LogEntry - Message (" Successfully removed application package: {0}" -f $AppPackageDetails .PackageFullName ) - Outhost
1128
1147
$c ++
1129
1148
}
1130
1149
catch [System.Exception ] {
1131
- Write-LogEntry - Message (" Failed removing AppxPackage: {0}" -f $_.Message ) - Severity 3 - Outhost
1150
+ Write-LogEntry - Message (" Failed removing AppxPackage: {0}" -f $_ ) - Severity 3 - Outhost
1151
+ }
1152
+ Finally {
1153
+ Write-LogEntry - Message (" --------------------------------------------------" ) - Outhost
1132
1154
}
1133
1155
}
1134
1156
else {
1135
- Write-LogEntry - Message (" Unable to locate AppxPackage for app: {0}" -f $App .Name ) - Outhost
1157
+ Write-LogEntry - Message (" Unable to locate AppxPackage for app: {0}" -f $AppPackageDetails .Name ) - Outhost
1136
1158
}
1137
1159
1138
1160
# Attempt to remove AppxProvisioningPackage
1139
- if ($null -eq $AppProvisioningPackageName ) {
1140
- Write-LogEntry - Message (" Removing application provisioning package: {0}" -f $AppProvisioningPackageName )
1161
+ if ($null -ne $AppProvisioningPackageName ) {
1162
+ Write-LogEntry - Message (" Removing application PROVISIONED package: {0}" -f $AppProvisioningPackageName )
1141
1163
try {
1142
1164
Remove-AppxProvisionedPackage - PackageName $AppProvisioningPackageName - Online - ErrorAction Stop | Out-Null
1143
- Write-LogEntry - Message (" Successfully removed application provisioning package: {0}" -f $AppProvisioningPackageName ) - Outhost
1165
+ Write-LogEntry - Message (" Successfully removed application PROVISIONED package: {0}" -f $AppProvisioningPackageName ) - Outhost
1166
+ $d ++
1144
1167
}
1145
1168
catch [System.Exception ] {
1146
- Write-LogEntry - Message (" Failed removing Appx Provisioning Package: {0}" -f $_.Message ) - Severity 3 - Outhost
1169
+ Write-LogEntry - Message (" Failed removing Appx PROVISIONED Package: {0}" -f $_ ) - Severity 3 - Outhost
1170
+ }
1171
+ Finally {
1172
+ Write-LogEntry - Message (" --------------------------------------------------" ) - Outhost
1147
1173
}
1148
1174
}
1149
1175
else {
1150
- Write-LogEntry - Message (" Unable to locate Appx Provisioning Package for app: {0}" -f $App .Name ) - Outhost
1176
+ Write-LogEntry - Message (" Unable to locate Appx PROVISIONED Package for app: {0}" -f $AppPackageDetails .Name ) - Outhost
1151
1177
}
1152
1178
1153
1179
}
1154
1180
1155
1181
$p ++
1156
1182
}
1157
1183
1158
- Write-LogEntry - Message (" Removed {0} built-in AppxPackage and AppxProvisioningPackage" -f $c ) - Outhost
1184
+ Write-LogEntry - Message (" Removed {0} All Users App Package's" -f $c ) - Outhost
1185
+ Write-LogEntry - Message (" Removed {0} built-in App PROVISIONED Package's" -f $d ) - Outhost
1159
1186
}
1160
1187
Else {$stepCounter ++ }
1161
1188
0 commit comments