Created
September 8, 2020 04:17
-
-
Save a-gu/4118f53a55dccfbdf1b8d6f86937958d to your computer and use it in GitHub Desktop.
PowerShell script to upgrade Chocolatey packages, and remove any added links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If running on a new machine, make sure to allow this script to run using the below commands or similar: | |
# Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy RemoteSigned | |
# Unblock-File -Path .\ChocoUpgradeAllNoLinks.ps1 | |
# powershell -File .\ChocoUpgradeAllNoLinks.ps1 | |
Set-StrictMode -Version 3.0 | |
# Snapshot Desktop links before upgrade | |
$links_before = Get-ChildItem "C:\Users\*\Desktop\*" -Filter "*.lnk" -File | |
# Upgrade packages | |
& "choco" upgrade all -y | |
# Snapshot Desktop links after upgrade | |
$links_after = Get-ChildItem "C:\Users\*\Desktop\*" -Filter "*.lnk" -File | |
# Handle added links | |
If ($links_after) { | |
If ($links_before) { | |
# Find added links | |
$diff = Compare-Object -ReferenceObject $links_before -DifferenceObject $links_after | |
$links_added = $diff | Where-Object { $_.SideIndicator -eq "=>" } | |
# Delete added links | |
If ($links_added) { | |
Remove-Item $links_added.InputObject | |
} | |
} else { | |
$links_after | Remove-Item | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment