-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSystemUpgrade.ps1
More file actions
1133 lines (977 loc) · 44.6 KB
/
SystemUpgrade.ps1
File metadata and controls
1133 lines (977 loc) · 44.6 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
A comprehensive script to update Windows, applications, and perform system maintenance.
.DESCRIPTION
This script provides functionality to update PowerShell modules, Windows Updates,
Microsoft Store applications, Winget packages, Chocolatey packages,
Pip packages, and NPM packages. It also includes tools to scan for and fix system
corruption, and to perform system cleanup by deleting temporary files and
emptying the Recycle Bin. The script supports both interactive and automated execution
through switch parameters.
.NOTES
PowerShell is run as an administrator is required for most operations in this script.
.LINK
https://github.com/get543/Windows-Scripting/blob/main/SystemUpgrade.ps1
#>
[CmdletBinding()]
param(
[Parameter(HelpMessage = "Upgrade all possible packages and modules.")]
[switch]$Upgrade,
[Parameter(HelpMessage = "Cleanup temporary files and folders.")]
[switch]$Cleanup,
[Parameter(HelpMessage = "Scan for system file corruption.")]
[switch]$Scan,
[Parameter(HelpMessage = "Automatically answer 'yes' to all interactive prompts.")]
[switch]$YesToAll,
[Parameter(HelpMessage = "Display this help message.")]
[switch]$Help
)
$Identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$Principal = New-Object Security.Principal.WindowsPrincipal $Identity
$IsAdmin = $Principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
function EmptyLine() {
Write-Host
}
<# -------------------------------------------------------- #>
<# Admin Relaunch & Checking #>
<# -------------------------------------------------------- #>
function NotAdminRelaunch() { #!BROKEN
<#
.SYNOPSIS
Admin or not ?
.DESCRIPTION
Check if the script executed with admin privilages or not.
If not, then script you immedietly exit and show error message.
.NOTES
Run in Main function when the script is executed.
#>
EmptyLine
Write-Host "Please run this script as an admin access." -ForegroundColor Red
Write-Host "Because almost all commands require admin access." -ForegroundColor Red
EmptyLine
Write-Host "Attempting to restart the script with admin access..." -ForegroundColor Yellow
$windowsTerminalInstalled = Get-Command -Name wt.exe -ErrorAction SilentlyContinue
$pwshInstalled = Get-Command -Name pwsh.exe -ErrorAction SilentlyContinue
$powershellInstalled = Get-Command -Name powershell.exe -ErrorAction SilentlyContinue
$powershellVersion = powershell.exe -Command "$PSVersionTable.PSVersion.ToString()"
EmptyLine
if ($windowsTerminalInstalled) {
Write-Host "Launching with Windows Terminal (wt.exe)..." -ForegroundColor Yellow
if ($pwshInstalled) {
Write-Host "Using $(pwsh.exe -v) as the shell for Windows Terminal." -ForegroundColor Yellow
Start-Process `
-FilePath "wt.exe" `
-ArgumentList "pwsh -ExecutionPolicy Bypass -File `"$PSCommandPath`" $($MyInvocation.UnboundArguments)" `
-Verb RunAs
} elseif ($powershellInstalled) {
Write-Host "Using PowerShell $powershellVersion as the shell for Windows Terminal." -ForegroundColor Yellow
Start-Process `
-FilePath "wt.exe" `
-ArgumentList "powershell -ExecutionPolicy Bypass -File `"$PSCommandPath`" $($MyInvocation.UnboundArguments)" `
-Verb RunAs
} else {
Write-Error "Cannot find any PowerShell executable. Please restart this script manually with admin access."
}
} elseif ($pwshInstalled -or $powershellInstalled) {
if ($pwshInstalled) {
Start-Process `
-FilePath "pwsh.exe" `
-ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`" $($MyInvocation.UnboundArguments)" `
-Verb RunAs
Write-Host "Launching with $(pwsh.exe -v) (pwsh.exe)..." -ForegroundColor Yellow
} elseif ($powershellInstalled) {
Write-Host "Launching with PowerShell $powershellVersion (powershell.exe)..." -ForegroundColor Yellow
Start-Process `
-FilePath "powershell.exe" `
-ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`" $($MyInvocation.UnboundArguments)" `
-Verb RunAs
} else {
Write-Error "Cannot find any PowerShell executable. Please restart this script manually with admin access."
}
}
}
<# -------------------------------------------------------- #>
<# Menu or Title #>
<# -------------------------------------------------------- #>
function HelpMenu() {
<#
.SYNOPSIS
Displays the help message for the script.
#>
EmptyLine
Write-Host "This script helps with system upgrade, cleanup, and maintenance tasks." -ForegroundColor Cyan
Write-Host "You can combine switches to perform multiple actions."
EmptyLine
Write-Host "PARAMETERS:" -ForegroundColor Green
Write-Host " -Upgrade Upgrade all possible packages and modules."
Write-Host " -Cleanup Cleanup temporary files and folders."
Write-Host " -Scan Scan for system file corruption."
Write-Host " -YesToAll Automatically answer 'yes' to all interactive prompts."
Write-Host " -Help Display this help message."
EmptyLine
Write-Host "EXAMPLES:" -ForegroundColor Green
Write-Host " .\SystemUpgrade.ps1 -Cleanup"
Write-Host " .\SystemUpgrade.ps1 -YesToAll"
Write-Host " .\SystemUpgrade.ps1 -Upgrade"
Write-Host " .\SystemUpgrade.ps1 -Scan"
Write-Host " Get-Help .\SystemUpgrade.ps1 -Full"
EmptyLine
}
function Title() {
<#
.SYNOPSIS
Shows the script title at the start of the script
#>
Clear-Host
Write-Host "
__| |_________________________________| |__
(__| |_________________________________| |__)
| | Windows System Upgrade Script | |
__| |_________________________________| |__
(__|_|_________________________________|_|__) " -ForegroundColor Magenta
}
function EndingScript() {
<#
.SYNOPSIS
Shows end message at the end of the script after all command has finished
#>
Write-Host "
__| |_________________________________| |__
(__| |_________________________________| |__)
| | End Script | |
__| |_________________________________| |__
(__|_|_________________________________|_|__) " -ForegroundColor Magenta
}
<# -------------------------------------------------------- #>
<# Update Function #>
<# -------------------------------------------------------- #>
function Invoke-PSModuleUpdate {
<#
.SYNOPSIS
Update PowerShell Module
.DESCRIPTION
This function updates all installed PowerShell modules if it needs to.
#>
if (!(Get-Command -Name Update-Module -ErrorAction SilentlyContinue)) {
EmptyLine
Write-Host "The 'Update-Module' command is not available. Skipping PowerShell module updates." -ForegroundColor Blue
return
}
$shouldUpdate = $false
if ($YesToAll.IsPresent -or $Upgrade.IsPresent) {
$shouldUpdate = $true
} else {
Write-Host "Update all PowerShell modules? [Y/n] " -ForegroundColor Blue -NoNewline
$UpdateModuleOption = Read-Host
if (($UpdateModuleOption.ToLower() -eq "y") -or ($UpdateModuleOption -eq "")) {
$shouldUpdate = $true
}
}
if ($shouldUpdate) {
EmptyLine
Write-Host "Updating all PowerShell modules..." -ForegroundColor Yellow
try {
Update-Module -AcceptLicense -ErrorAction Stop
}
catch {
Write-Warning "An error occurred while updating PowerShell modules."
Write-Warning $_.Exception.Message
}
} else {
EmptyLine
Write-Host "Skipping PowerShell module updates." -ForegroundColor Yellow
}
}
function Invoke-WindowsUpdate {
<#
.SYNOPSIS
Checks for, installs, and manages Windows Updates using the PSWindowsUpdate module.
#>
# Decide whether to run interactively or automatically
$isInteractive = (-not ($YesToAll.IsPresent -or $Upgrade.IsPresent))
# Check for PSWindowsUpdate module and offer to install if missing
if (!(Get-Module -Name "PSWindowsUpdate" -ListAvailable -ErrorAction SilentlyContinue) -and $isInteractive) {
$installPrompt = "The 'PSWindowsUpdate' module is required to manage Windows Updates. Install it now? [Y/n]"
$shouldInstall = $false
if ($YesToAll.IsPresent) {
$shouldInstall = $true
}
else {
$installChoice = Read-Host -Prompt $installPrompt
if (($installChoice.ToLower() -eq 'y') -or ($installChoice -eq '')) {
$shouldInstall = $true
}
}
if ($shouldInstall) {
EmptyLine
Write-Host "Installing 'PSWindowsUpdate' module..." -ForegroundColor Yellow
try {
Install-Module -Name PSWindowsUpdate -Force -AcceptLicense -Confirm:$false
}
catch {
Write-Warning "Failed to install 'PSWindowsUpdate' module."
Write-Warning $_.Exception.Message
return
}
}
else {
EmptyLine
Write-Host "Skipping Windows Update checks because 'PSWindowsUpdate' module is not installed." -ForegroundColor Yellow
return
}
}
# Import the module to ensure commands are available
try {
Import-Module -Name PSWindowsUpdate -Force -ErrorAction Stop
}
catch {
Write-Warning "Failed to import 'PSWindowsUpdate' module."
Write-Warning $_.Exception.Message
return
}
# Check if Install-WindowsUpdate command exists
if (!(Get-Command -Name "Install-WindowsUpdate" -ErrorAction SilentlyContinue)) {
EmptyLine
Write-Host "The 'Install-WindowsUpdate' command is not available in this version of PSWindowsUpdate." -ForegroundColor Red
Write-Host "Windows Update management may not be supported on this system." -ForegroundColor Red
Write-Host "Try updating the PSWindowsUpdate module or using Windows Update settings directly." -ForegroundColor Yellow
return
}
try {
if ($isInteractive) {
#! Interactive Mode
# Ask user if they want to check for Windows Updates before proceeding with installation
Write-Host "Check for Windows Updates ? [Y/n] " -ForegroundColor Blue -NoNewline
$windowsUpdateOption = Read-Host
if (($windowsUpdateOption.ToLower() -eq "y") -or ($windowsUpdateOption -eq "")) {
do {
Clear-Host
Write-Host "Checking for Windows Updates..." -ForegroundColor Yellow
$updates = Get-WindowsUpdate
$updates | Format-Table
if (!$updates) {
EmptyLine
Write-Host "No Windows Updates found." -ForegroundColor Green
Start-Sleep -Seconds 3
break
}
EmptyLine
Write-Host "Enter the KB Article ID to install. Separate multiple IDs with a space." -ForegroundColor Green
Write-Host "Type 'all' to install all updates, or 'exit' to skip." -ForegroundColor Green
Write-Host "Example : KB5026958 KB5026958 KB5025233" -ForegroundColor Green
$updateChoice = Read-Host -Prompt "KB Article ID "
if ($updateChoice.ToLower() -eq 'exit' -or [string]::IsNullOrEmpty($updateChoice)) {
EmptyLine
Write-Host "Exiting Windows Update." -ForegroundColor Yellow
break
}
if ($updateChoice.ToLower() -eq 'all') {
EmptyLine
Write-Host "Installing all available Windows Updates..." -ForegroundColor Yellow
try {
Install-WindowsUpdate -AcceptAll -IgnoreReboot
}
catch {
Write-Warning "Install-WindowsUpdate command failed."
Write-Warning $_.Exception.Message
}
}
else {
$ArrayID = $updateChoice.Split(" ")
EmptyLine
Write-Host "Installing selected Windows Updates..." -ForegroundColor Yellow
try {
Install-WindowsUpdate -KBArticleID $ArrayID -AcceptAll -IgnoreReboot
}
catch {
Write-Warning "Install-WindowsUpdate command failed for specific KBs."
Write-Warning $_.Exception.Message
}
}
EmptyLine
Write-Host "Update process finished. Re-checking for more updates..." -ForegroundColor Yellow
Start-Sleep -Seconds 3
} while ($true)
} else {
EmptyLine
Write-Host "Skipping Windows Update checks." -ForegroundColor Yellow
}
}
else {
#! Automatic mode
EmptyLine
Write-Host "Checking for and installing all Windows Updates automatically..." -ForegroundColor Yellow
try {
Install-WindowsUpdate -Verbose -AcceptAll -IgnoreReboot
}
catch {
Write-Warning "Install-WindowsUpdate command failed."
Write-Warning $_.Exception.Message
}
EmptyLine
Write-Host "Automatic Windows Update process completed." -ForegroundColor Green
}
}
catch {
EmptyLine
Write-Warning "An error occurred during the Windows Update process."
Write-Warning $_.Exception.Message
}
}
function Invoke-MSStoreUpdate {
<#
.SYNOPSIS
Updates Microsoft Store applications using winget.
#>
if (!(Get-Command -Name Update-InboxApp)) {
$installPrompt = "The 'Update-InboxApp' command is not available. Install it now? [Y/n] "
$shouldInstall = $false
if ($YesToAll.IsPresent) {
$shouldInstall = $true
}
else {
$installChoice = Read-Host -Prompt $installPrompt
if (($installChoice.ToLower() -eq 'y') -or ($installChoice -eq '')) {
$shouldInstall = $true
}
}
if ($shouldInstall) {
EmptyLine
Write-Host "Installing 'Update-InboxApp' script..." -ForegroundColor Yellow
try {
Install-Script Update-InboxApp
}
catch {
Write-Warning "Failed to install 'Update-InboxApp' script."
Write-Warning $_.Exception.Message
return
}
}
else {
EmptyLine
Write-Host "Skipping Windows Update checks because 'Update-InboxApp' script is not installed." -ForegroundColor Yellow
return
}
EmptyLine
Write-Host "The 'Update-InboxApp' command is not available. Skipping Microsoft Store app updates." -ForegroundColor Blue
return
}
$shouldUpdate = $false
if ($YesToAll.IsPresent -or $Upgrade.IsPresent) {
$shouldUpdate = $true
}
else {
Write-Host "Update all Microsoft Store applications? [Y/n] " -ForegroundColor Blue -NoNewline
$updateChoice = Read-Host
if (($updateChoice.ToLower() -eq 'y') -or ($updateChoice -eq '')) {
$shouldUpdate = $true
}
}
if ($shouldUpdate) {
EmptyLine
Write-Host "Updating all Microsoft Store applications..." -ForegroundColor Yellow
try {
powershell.exe -Command "Get-AppxPackage | Update-InboxApp"
}
catch {
Write-Warning "An error occurred while updating Microsoft Store applications."
Write-Warning $_.Exception.Message
}
}
else {
EmptyLine
Write-Host "Skipping Microsoft Store application updates." -ForegroundColor Yellow
}
}
function Invoke-WingetUpdate {
<#
.SYNOPSIS
Installs or updates winget, and then updates all winget packages.
#>
# Decide whether to run interactively or automatically
$isInteractive = (-not ($YesToAll.IsPresent -or $Upgrade.IsPresent))
# Check for winget and offer to install if missing (interactive mode)
if (!(Get-Command -Name winget -ErrorAction SilentlyContinue) -and $isInteractive) {
$installPrompt = "The 'winget' command is not available. Install it now ? [Y/n] "
$shouldInstall = $false
if ($YesToAll.IsPresent) {
$shouldInstall = $true
}
else {
$installChoice = Read-Host -Prompt $installPrompt
if (($installChoice.ToLower() -eq 'y') -or ($installChoice -eq '')) {
$shouldInstall = $true
}
}
if ($shouldInstall) {
EmptyLine
Write-Host "Installing 'winget'..." -ForegroundColor Yellow
try {
# Get the latest release from the official GitHub repository
$release = Invoke-RestMethod -Uri "https://api.github.com/repos/microsoft/winget-cli/releases/latest"
$asset = $release.assets | Where-Object { $_.name -like '*.msixbundle' } | Select-Object -First 1
$downloadUrl = $asset.browser_download_url
$downloadPath = Join-Path $env:TEMP $asset.name
Invoke-WebRequest -Uri $downloadUrl -OutFile $downloadPath
Add-AppxPackage -Path $downloadPath
}
catch {
EmptyLine
Write-Warning "Failed to install 'winget'. "
Write-Warning $_.Exception.Message
return
}
}
else {
EmptyLine
Write-Host "Skipping winget package updates because 'winget' is not installed." -ForegroundColor Yellow
return
}
}
try {
if ($isInteractive) {
#! Interactive Mode
# Ask user if they want to check for winget updates before proceeding with package upgrades
Write-Host "Check for Winget Updates ? [Y/n] " -ForegroundColor Blue -NoNewline
$updateWingetOption = Read-Host
if (($updateWingetOption.ToLower() -eq "y") -or ($updateWingetOption -eq "")) {
do {
Clear-Host
Write-Host "Checking for upgradable winget applications..." -ForegroundColor Yellow
winget upgrade --include-unknown
EmptyLine
Write-Host "Enter the App ID to upgrade. Separate multiple IDs with a space." -ForegroundColor Green
Write-Host "Type 'all' to upgrade all applications, or 'exit' to skip." -ForegroundColor Green
Write-Host "Example : Microsoft.VisualStudioCode Microsoft.PowerShell Obsidian.Obsidian" -ForegroundColor Green
$updateChoice = Read-Host -Prompt "App ID "
if ($updateChoice.ToLower() -eq 'exit' -or [string]::IsNullOrEmpty($updateChoice)) {
EmptyLine
Write-Host "Exiting winget upgrade." -ForegroundColor Yellow
break
}
if ($updateChoice.ToLower() -eq 'all') {
EmptyLine
Write-Host "Upgrading all applications..." -ForegroundColor Yellow
winget upgrade --all --include-unknown --accept-package-agreements --accept-source-agreements
}
else {
$ArrayID = $updateChoice.Split(" ")
EmptyLine
Write-Host "Upgrading selected applications..." -ForegroundColor Yellow
foreach ($appId in $ArrayID) {
winget upgrade --id $appId --include-unknown --accept-package-agreements --accept-source-agreements
}
}
EmptyLine
Write-Host "Winget upgrade process finished. Re-checking for more updates..." -ForegroundColor Yellow
Start-Sleep -Seconds 3
} while ($true)
} else {
EmptyLine
Write-Host "Skipping winget package updates." -ForegroundColor Yellow
}
}
else {
#! Automatic mode
EmptyLine
Write-Host "Checking for and upgrading all winget packages automatically..." -ForegroundColor Yellow
winget upgrade --all --include-unknown --accept-package-agreements --accept-source-agreements
EmptyLine
Write-Host "Automatic winget upgrade complete." -ForegroundColor Green
}
}
catch {
EmptyLine
Write-Warning "An error occurred during the winget upgrade process."
Write-Warning $_.Exception.Message
}
}
function Invoke-ChocolateyUpdate {
<#
.SYNOPSIS
Installs or updates Chocolatey, and then updates all Chocolatey packages.
#>
# Decide whether to run interactively or automatically
$isInteractive = (-not ($YesToAll.IsPresent -or $Upgrade.IsPresent))
# Check for Chocolatey and offer to install if missing (interactive)
if (!(Get-Command -Name choco -ErrorAction SilentlyContinue) -and $isInteractive) {
$installPrompt = "The 'choco' command is not available. Install it now ? [Y/n] "
$shouldInstall = $false
if ($YesToAll.IsPresent) {
$shouldInstall = $true
}
else {
$installChoice = Read-Host -Prompt $installPrompt
if (($installChoice.ToLower() -eq 'y') -or ($installChoice -eq '')) {
$shouldInstall = $true
}
}
EmptyLine
if ($shouldInstall) {
Write-Host "Installing 'Chocolatey'..." -ForegroundColor Yellow
try {
Set-ExecutionPolicy Bypass -Scope Process -Force
$installScript = Join-Path $env:TEMP "install-choco.ps1"
Invoke-WebRequest -Uri 'https://chocolatey.org/install.ps1' -OutFile $installScript
& $installScript
}
catch {
Write-Warning "Failed to install 'Chocolatey'. $_"
return
}
}
else {
Write-Host "Skipping Chocolatey package updates because 'choco' is not installed." -ForegroundColor Yellow
return
}
}
try {
if ($isInteractive) {
#! Interactive Mode
# Ask user if they want to check for Chocolatey updates before proceeding with package upgrades
Write-Host "Check for Chocolatey Updates ? [Y/n] " -ForegroundColor Blue -NoNewline
$updateChocoOption = Read-Host
if (($updateChocoOption.ToLower() -eq "y") -or ($updateChocoOption -eq "")) {
do {
Clear-Host
Write-Host "Checking for outdated Chocolatey packages..." -ForegroundColor Yellow
choco outdated
EmptyLine
Write-Host "Enter the package name to upgrade. Separate multiple names with a space." -ForegroundColor Green
Write-Host "Type 'all' to upgrade all packages, or 'exit' to skip." -ForegroundColor Green
Write-Host "Example : python hwinfo chocolatey" -ForegroundColor Green
$updateChoice = Read-Host -Prompt "Package Name"
if ($updateChoice.ToLower() -eq 'exit' -or [string]::IsNullOrEmpty($updateChoice)) {
EmptyLine
Write-Host "Exiting Chocolatey upgrade." -ForegroundColor Yellow
break
}
EmptyLine
if ($updateChoice.ToLower() -eq 'all') {
Write-Host "Upgrading all packages..." -ForegroundColor Yellow
choco upgrade all -y
}
else {
$ArrayID = $updateChoice.Split(" ")
Write-Host "Upgrading selected packages..." -ForegroundColor Yellow
foreach ($pkg in $ArrayID) {
choco upgrade $pkg -y
}
}
EmptyLine
Write-Host "Chocolatey upgrade process finished. Re-checking for more outdated packages..." -ForegroundColor Yellow
Start-Sleep -Seconds 3
} while ($true)
} else {
EmptyLine
Write-Host "Skipping Chocolatey package updates." -ForegroundColor Yellow
}
}
else {
#! Automatic mode
EmptyLine
Write-Host "Checking for and upgrading all Chocolatey packages automatically..." -ForegroundColor Yellow
choco upgrade all -y
EmptyLine
Write-Host "Automatic Chocolatey upgrade complete." -ForegroundColor Green
}
}
catch {
EmptyLine
Write-Warning "An error occurred during the Chocolatey upgrade process."
Write-Warning $_.Exception.Message
}
}
<# -------------------------------------------------------- #>
<# System Maintenance Tools #>
<# -------------------------------------------------------- #>
function Invoke-SystemScan {
<#
.SYNOPSIS
Scans for and attempts to fix system corruption files.
#>
$shouldScan = $false
if ($YesToAll.IsPresent -or $Scan.IsPresent) {
$shouldScan = $true
} else {
Write-Host "Check for system corruption files? [Y/n] " -ForegroundColor Blue -NoNewline
$scanOption = Read-Host
if (($scanOption.ToLower() -eq "y") -or ($scanOption -eq "")) {
$shouldScan = $true
}
}
if ($shouldScan) {
EmptyLine
Write-Host "(1/4) Running 'chkdsk /scan' (check disk)..." -ForegroundColor Yellow
chkdsk /scan
EmptyLine
Write-Host "(2/4) Running 'sfc /SCANNOW' (System File Checker) - 1st scan..." -ForegroundColor Yellow
sfc /SCANNOW
EmptyLine
Write-Host "(3/4) Running DISM (Deployment Image Servicing and Management tool)..." -ForegroundColor Yellow
DISM /Online /Cleanup-Image /Restorehealth
EmptyLine
Write-Host "(4/4) Running 'sfc /SCANNOW' (System File Checker) - 2nd scan..." -ForegroundColor Yellow
sfc /SCANNOW
EmptyLine
Write-Host "System corruption scan complete." -ForegroundColor Green
} else {
EmptyLine
Write-Host "Skipping system corruption file scan." -ForegroundColor Yellow
}
}
function Invoke-SystemCleanup {
<#
.SYNOPSIS
Deletes temporary files, old Windows Update files, and empties the Recycle Bin.
.NOTES
This function will attempt to run Disk Cleanup, delete temporary files from both
Cleaning $env:windir\Temp and $env:TEMP, and empty the Recycle Bin.
#>
$shouldCleanup = $false
if ($YesToAll.IsPresent -or $Cleanup.IsPresent) {
$shouldCleanup = $true
} else {
Write-Host "Delete unused files and folders ? [Y/n] " -ForegroundColor Blue -NoNewline
$cleanupOption = Read-Host
if (($cleanupOption.ToLower() -eq "y") -or ($cleanupOption -eq "")) {
$shouldCleanup = $true
}
}
if ($shouldCleanup) {
# Run Disk Cleanup
EmptyLine
Write-Host "Running Disk Cleanup..." -ForegroundColor Yellow
try {
if ((Get-Command -Name cleanmgr -ErrorAction SilentlyContinue) -and (Test-Path "$env:windir\system32\cleanmgr.exe")) {
cleanmgr.exe /d $env:HOMEDRIVE /VERYLOWDISK
}
else {
Write-Warning "'cleanmgr' command not found. Skipping Disk Cleanup."
}
}
catch {
Write-Warning "An error occurred during Disk Cleanup. $_.Exception.Message"
}
# Delete Temporary Files
EmptyLine
Write-Host "Deleting Temporary Files..." -ForegroundColor Yellow
try {
if ((Test-Path "$env:windir\Temp") -and (Test-Path "$env:TEMP")) {
Get-ChildItem -Path "$env:windir\Temp" -File -Recurse -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path "$env:windir\Temp" -Directory -Recurse -ErrorAction SilentlyContinue | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
Get-ChildItem -Path $env:TEMP -File -Recurse -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path $env:TEMP -Directory -Recurse -ErrorAction SilentlyContinue | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
}
else {
Write-Warning "Temporary directories not found. Skipping temporary file deletion."
}
}
catch {
Write-Warning "An error occurred during temporary file deletion. $_.Exception.Message"
}
# Empty Recycle Bin
EmptyLine
Write-Host "Emptying Recycle Bin..." -ForegroundColor Yellow
try {
if (Get-Command -Name Clear-RecycleBin -ErrorAction SilentlyContinue) {
Clear-RecycleBin -Force -ErrorAction Stop
}
else {
Write-Warning "'Clear-RecycleBin' command not found. Skipping Recycle Bin emptying."
}
}
catch {
EmptyLine
Write-Warning "An error occurred while emptying the Recycle Bin. $_.Exception.Message"
}
EmptyLine
Write-Host "System cleanup complete." -ForegroundColor Green
} else {
EmptyLine
Write-Host "Skipping system cleanup." -ForegroundColor Yellow
}
}
<# -------------------------------------------------------- #>
<# DevTools #>
<# -------------------------------------------------------- #>
function Invoke-PipUpgrade {
<#
.SYNOPSIS
Upgrades outdated pip packages.
#>
if (!(Get-Command -Name pip -ErrorAction SilentlyContinue)) {
EmptyLine
Write-Host "The 'pip' command is not available. Skipping pip package upgrades." -ForegroundColor Blue
return
}
# Decide whether to run interactively or automatically
$isInteractive = (-not ($YesToAll.IsPresent -or $Upgrade.IsPresent))
EmptyLine
try {
if ($isInteractive) {
#! Interactive Mode
# Ask user if they want to check for pip updates before proceeding with package upgrades
Write-Host "Check for pip Updates ? [Y/n] " -ForegroundColor Blue -NoNewline
$updatePipOption = Read-Host
if (($updatePipOption.ToLower() -eq "y") -or ($updatePipOption -eq "")) {
do {
Clear-Host
Write-Host "Updating pip itself..." -ForegroundColor Yellow
python.exe -m pip install --upgrade pip
EmptyLine
Write-Host "Checking for outdated pip packages..." -ForegroundColor Yellow
$outdated = pip list --outdated
if ($outdated) {
$outdated
} else {
EmptyLine
Write-Host "No outdated pip packages found." -ForegroundColor Green
Start-Sleep -Seconds 3
break
}
EmptyLine
Write-Host "Enter the package name to upgrade. Separate multiple names with a space." -ForegroundColor Green
Write-Host "Type 'all' to upgrade all packages, or 'exit' to skip." -ForegroundColor Green
Write-Host "Example : requests numpy pandas" -ForegroundColor Green
$updateChoice = Read-Host -Prompt "Package Name "
EmptyLine
if ($updateChoice.ToLower() -eq 'exit' -or [string]::IsNullOrEmpty($updateChoice)) {
Write-Host "Exiting pip upgrade." -ForegroundColor Yellow
break
}
if ($updateChoice.ToLower() -eq 'all') {
Write-Host "Upgrading all packages..." -ForegroundColor Yellow
pip list --outdated --format=json | ConvertFrom-Json | ForEach-Object { pip install --upgrade $_.name }
}
else {
$packageNames = $updateChoice.Split(" ")
Write-Host "Upgrading selected packages..." -ForegroundColor Yellow
foreach ($pkg in $packageNames) {
pip install --upgrade $pkg
}
}
EmptyLine
Write-Host "Pip upgrade process finished. Re-checking for more outdated packages..." -ForegroundColor Yellow
Start-Sleep -Seconds 3
} while ($true)
} else {
EmptyLine
Write-Host "Skipping pip package updates." -ForegroundColor Yellow
return
}
}
else {
#! Automatic mode
EmptyLine
Write-Host "Updating pip itself..." -ForegroundColor Yellow
python.exe -m pip install --upgrade pip
EmptyLine
Write-Host "Checking for and upgrading all pip packages automatically..." -ForegroundColor Yellow
pip list --outdated --format=json | ConvertFrom-Json | ForEach-Object { pip install --upgrade $_.name }
EmptyLine
Write-Host "Automatic pip upgrade complete." -ForegroundColor Green
}
}
catch {
EmptyLine
Write-Warning "An error occurred during the pip upgrade process."
Write-Warning $_.Exception.Message
}
}
function Invoke-NpmUpgrade {
<#
.SYNOPSIS
Upgrades outdated npm packages.
#>
if (!(Get-Command -Name npm -ErrorAction SilentlyContinue)) {
EmptyLine
Write-Host "The 'npm' command is not available. Skipping npm package upgrades." -ForegroundColor Blue
return
}
# Decide whether to run interactively or automatically
$isInteractive = (-not ($YesToAll.IsPresent -or $Upgrade.IsPresent))
try {
if ($isInteractive) {
#! Interactive Mode
EmptyLine
# Ask user if they want to check for Chocolatey updates before proceeding with package upgrades
Write-Host "Check for npm Updates ? [Y/n] " -ForegroundColor Blue -NoNewline
$updateNpmOption = Read-Host
if (($updateNpmOption.ToLower() -eq "y") -or ($updateNpmOption -eq "")) {
do {
Clear-Host
npm -g outdated | Select-Object -Skip 1 | ForEach-Object {
$package = ($_ -split '\s+')[0]
if ($package -eq "npm") {
Write-Host "Updating npm itself..." -ForegroundColor Yellow
npm install -g npm@latest
} else {
Write-Host "No NPM upgrade needed..." -ForegroundColor Yellow
}
}
if ((Get-ChildItem -Filter "package.json" -ErrorAction SilentlyContinue) -or
(Get-ChildItem -Filter "package-lock.json" -ErrorAction SilentlyContinue) -or
(Get-ChildItem -Filter "node_modules" -ErrorAction SilentlyContinue)) {
EmptyLine
Write-Host "Checking for outdated local npm packages..." -ForegroundColor Yellow
npm outdated
} else {
EmptyLine
Write-Host "Checking for outdated global npm packages..." -ForegroundColor Yellow
npm -g outdated
}
EmptyLine
Write-Host "Enter the package name to upgrade. Separate multiple names with a space." -ForegroundColor Green
Write-Host "Type 'all-global' to upgrade all global packages, 'all-local' for local, or 'all' for both." -ForegroundColor Green
Write-Host "Type 'all-latest' to upgrade all local packages to the latest version." -ForegroundColor Green
Write-Host "Type 'exit' to skip." -ForegroundColor Green
Write-Host "Example : express react typescript" -ForegroundColor Green
$updateChoice = Read-Host -Prompt "Package Name"
if ($updateChoice.ToLower() -eq 'exit' -or [string]::IsNullOrEmpty($updateChoice)) {
EmptyLine
Write-Host "Exiting npm upgrade." -ForegroundColor Yellow
break
}
if ($updateChoice.ToLower() -eq 'all' -or $updateChoice.ToLower() -eq 'all-global') {
EmptyLine
Write-Host "Upgrading all global packages..." -ForegroundColor Yellow
npm -g update --all
}
if ($updateChoice.ToLower() -eq 'all' -or $updateChoice.ToLower() -eq 'all-local') {
EmptyLine
Write-Host "Upgrading all local packages..." -ForegroundColor Yellow
npm update --all
}
if ($updateChoice.ToLower() -eq 'all-latest') {
EmptyLine
Write-Host "Upgrading all local packages to the latest version..." -ForegroundColor Yellow
npm outdated | Select-Object -Skip 1 | ForEach-Object { $package = ($_ -split '\s+')[0]; npm install $package@latest }
}
if (($updateChoice.ToLower() -ne 'all') -and
($updateChoice.ToLower() -ne 'all-local') -and
($updateChoice.ToLower() -ne 'all-global') -and
($updateChoice.ToLower() -ne 'all-latest')) {
$packageNames = $updateChoice.Split(" ")
EmptyLine
Write-Host "Upgrading selected packages..." -ForegroundColor Yellow
foreach ($pkg in $packageNames) {
# A bit tricky to know if it's global or local, so we can try local first, then global.
if ((Get-ChildItem -Filter "package.json" -ErrorAction SilentlyContinue) -or
(Get-ChildItem -Filter "package-lock.json" -ErrorAction SilentlyContinue) -or
(Get-ChildItem -Filter "node_modules" -ErrorAction SilentlyContinue)) {
EmptyLine
Write-Host "Attempting to upgrade '$pkg' locally..."
npm update $pkg
} else {
EmptyLine
Write-Host "Attempting to upgrade '$pkg' globally..."
npm -g update $pkg
}
}