-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdate-Software.ps1
More file actions
98 lines (79 loc) · 3.61 KB
/
Copy pathUpdate-Software.ps1
File metadata and controls
98 lines (79 loc) · 3.61 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<#
.SYNOPSIS
Updates all the software on the system with chocolatey and winget with one command.
.DESCRIPTION
Updates all the software on the system with choco and winget by calling both package managers and updates the system.
.PARAMETER Confirm
Updates all the software on the system with chocolatey and winget and you need to confirm each step
.PARAMETER CheckIfChocolateyIsInstalled
Updates all the software on the system with chocolatey and winget and checks if you have chocolatey installed first. If not, it will only update winget packages.
.EXAMPLE
PS> Update-Software.ps1
Updates all the software on the system by chocolatey and winget
.EXAMPLE
PS> Update-Software -Confirm
Updates all the software and you need to confirm each step
.EXAMPLE
PS> Update-Software -CheckIfChocolateyIsInstalled
Updates all the software and check if Chocolatey is installed first and only updates winget packages.
#>
#Parameters
[CmdletBinding(DefaultParameterSetName = "System")]
param (
[Parameter(Mandatory=$false)]
[switch]
$Confirm,
[Parameter(Mandatory=$false)]
[switch]
$CheckIfChocolateyIsInstalled
)
# Test admin privileges without using -Requires RunAsAdministrator,
# which causes a nasty error message, if trying to load the function within a PS profile but without admin privileges
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) {
Write-Warning ("Function {0} needs admin privileges. Break now." -f $MyInvocation.MyCommand)
return
}
# flag for installing chocolatey
$HasChocolateyBeenInstalled=$true
if ($CheckIfChocolateyIsInstalled) {
Write-Host ("Checking if Chocolatey is installed in $env:ProgramData\Chocolatey folder") -ForegroundColor Green
if (-not (Test-Path -Path "$env:ProgramData\Chocolatey")) {
# check if choco.exe exists in the path
if (Get-Command choco.exe -ErrorAction SilentlyContinue) {
$HasChocolateyBeenInstalled = $true
Write-Host ("Chocolatey choco.exe command is installed in the system") -ForegroundColor Green
}
else {
$HasChocolateyBeenInstalled = $false
Write-Host ("Chocolatey is not installed. Please install it first at https://chocolatey.org to update choco packages....") -ForegroundColor Red
}
}
else {
Write-Host ("Chocolatey is installed, continuing with upgrading process....") -ForegroundColor Green
}
}
Write-Host ("Starting with updating winget packages....") -ForegroundColor Green
if ($Confirm) {
winget upgrade --all
Write-Host ("Starting with updating chocolatey packages and you will need to confirm each step...") -ForegroundColor Green
if ($HasChocolateyBeenInstalled) {
choco upgrade all
}
else {
Write-Host ("Chocolatey is not installed. Please install it first. Continuing with updating winget packages...") -ForegroundColor Red
}
}
else {
# Update winget packages
winget upgrade --all --accept-source-agreements
Write-Host ("Starting with updating chocolatey packages...") -ForegroundColor Green
if ($HasChocolateyBeenInstalled) {
choco upgrade all -y
}
}
if ($HasChocolateyBeenInstalled) {
Write-Host ("Done with updating all software on the system with chocolatey and winget.") -ForegroundColor Green
}
else {
Write-Host ("Done with updating all software on the system with winget. To be able to run it with Chocolatey, you need to have that installed first at https://chocolatey.org.") -ForegroundColor Green
}