77 Utilizes LGPO.exe to apply group policy item where neceassary.
88 Utilizes MDT/SCCM TaskSequence property control
99 Configurable using custom variables in MDT/SCCM
10+
11+ . EXAMPLE
12+ powershell.exe -ExecutionPolicy Bypass -file "AppOptimizeAndConfig.ps1"
13+
1014 .INFO
15+ Script: AppOptimizeAndConfig.ps1
1116 Author: Richard Tracy
1217 Email: richard.tracy@hotmail.com
1318 Twitter: @rick2_1979
@@ -237,7 +242,7 @@ Function Write-LogEntry{
237242Out-File - InputObject$LogFormat - Append- NoClobber- EncodingDefault - FilePath$OutputLogFile - ErrorAction Stop
238243 }
239244catch {
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
241246 }
242247 }
243248End {
@@ -516,17 +521,20 @@ Function Set-SystemSetting {
516521# verify the registry value has been set
517522Try {
518523If (-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 )- Severity0 - 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 )- Severity0 - 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
522530 }
523531Else {
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 - ErrorActionSilentlyContinue - 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 - ErrorActionStop | Out-Null
526534 }
527535 }
528536Catch {
529- Write-LogEntry (" Unable toset registry key [{0}\{1}\{2}] with value [{3}] " -f $RegHive , $RegKeyPath , $RegKeyName , $Value )- Severity2 - Source${CmdletName}
537+ Write-LogEntry (" Unable toconfigure registry key [{0}\{1}\{2}]. {4} " -f $RegHive , $RegKeyPath , $RegKeyName , $Value , $_ .Exception.Message )- Severity3 - Source${CmdletName}
530538 }
531539
532540 }
@@ -701,7 +709,12 @@ Function Set-UserSetting {
701709If ($HiveLoaded -eq $true ) {
702710If ($Message ){Write-LogEntry (" {0} for User [{1}].." -f $Message , $UserName )}
703711If ($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 )- Severity3 - Source${CmdletName}
717+ }
705718 }
706719Else {
707720Set-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 {
720733Else {
721734If ($Message ){Write-LogEntry (" {0} for [{1}].." -f $Message , $ProfileList.UserName )}
722735If ($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 )- Severity3 - Source${CmdletName}
741+ }
724742 }
725743Else {
726744Set-SystemSetting - Path" $RegHive \$RegKeyPath " - Name$RegKeyName - Type$Type - Value$Value - Force:$Force - WhatIf:$WhatIfPreference - TryLGPO:$TryLGPO
@@ -1104,6 +1122,7 @@ If($RemoveAppxPackages)
11041122
11051123$p = 1
11061124$c = 0
1125+ $d = 0
11071126# Loop through the list of appx packages
11081127foreach ($App in $AppArrayList ) {
11091128
@@ -1113,49 +1132,57 @@ If($RemoveAppxPackages)
11131132 }
11141133else {
11151134# Gather package names
1116- $AppPackageFullName = Get-AppxPackage - Name$App.Name | Select-Object - ExpandProperty PackageFullName
1135+ $AppPackageDetails = Get-AppxPackage - AllUsers - Name$App.Name
11171136
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
11191138
11201139# 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
11231142
11241143try {
1125- Remove-AppxPackage - Package$AppPackageFullName - ErrorAction Stop| Out-Null
1144+ Remove-AppxPackage - AllUsers - Package$AppPackageDetails .PackageFullName - ErrorAction Stop| Out-Null
11261145
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
11281147$c ++
11291148 }
11301149catch [System.Exception ] {
1131- Write-LogEntry - Message (" Failed removing AppxPackage: {0}" -f $_.Message )- Severity3 - Outhost
1150+ Write-LogEntry - Message (" Failed removing AppxPackage: {0}" -f $_ )- Severity3 - Outhost
1151+ }
1152+ Finally {
1153+ Write-LogEntry - Message (" --------------------------------------------------" )- Outhost
11321154 }
11331155 }
11341156else {
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
11361158 }
11371159
11381160# Attempt to remove AppxProvisioningPackage
1139- if ($null -eq $AppProvisioningPackageName ) {
1140- Write-LogEntry - Message (" Removing applicationprovisioning package: {0}" -f $AppProvisioningPackageName )
1161+ if ($null -ne $AppProvisioningPackageName ) {
1162+ Write-LogEntry - Message (" Removing applicationPROVISIONED package: {0}" -f $AppProvisioningPackageName )
11411163try {
11421164Remove-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 ++
11441167 }
11451168catch [System.Exception ] {
1146- Write-LogEntry - Message (" Failed removing Appx Provisioning Package: {0}" -f $_.Message )- Severity3 - Outhost
1169+ Write-LogEntry - Message (" Failed removing Appx PROVISIONED Package: {0}" -f $_ )- Severity3 - Outhost
1170+ }
1171+ Finally {
1172+ Write-LogEntry - Message (" --------------------------------------------------" )- Outhost
11471173 }
11481174 }
11491175else {
1150- Write-LogEntry - Message (" Unable to locate AppxProvisioning Package for app: {0}" -f $App .Name )- Outhost
1176+ Write-LogEntry - Message (" Unable to locate AppxPROVISIONED Package for app: {0}" -f $AppPackageDetails .Name )- Outhost
11511177 }
11521178
11531179 }
11541180
11551181$p ++
11561182 }
11571183
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
11591186}
11601187Else {$stepCounter ++ }
11611188