-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSystemUpgrade.ps1
1006 lines (821 loc) · 33.9 KB
/
SystemUpgrade.ps1
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
<#
TODO ValidateSet: Find a way to make it shorter
- (https://adamtheautomator.com/powershell-validateset/)
- (https://java2blog.com/check-if-array-contains-element-powershell/)
- (https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.3&viewFallbackFrom=powershell-7.1#dynamic-validateset-values)
TODO Help Menu: Find a way to make it prettier
#>
<#
.DESCRIPTION
This is a script that will update windows and applications on your system, if you install all the required powershell module. This will also update all the powershell module that is installed on your system.
.PARAMETER Help
Display a help message which is basically the same thing as this one. Valid values : all
.PARAMETER Option
Open up an option on how you want to run the script. Valid values : yes, assume-yes, assumeyes, answersyes, answers-yes, half-yes, normal, regular
.EXAMPLE
.\SystemUpgrade.ps1 -help all
.EXAMPLE
.\SystemUpgrade.ps1 -option yes
.EXAMPLE
.\SystemUpgrade.ps1 -option normal
.SYNOPSIS
Use to update windows and/or update your applications.
And with extra tools, like clean all temporary folders and old Windows Updates.
And another extra tools is check for system corruption files.
#>
param(
[Parameter(ParameterSetName = 'Option')]
[ValidateSet(
"yes",
"assume-yes",
"assumeyes",
"answersyes",
"answers-yes",
"half-yes",
"normal",
"regular",
"update",
"upgrade",
"cleanup",
"deletetempfiles",
"deletetemp"
)]
[String] $Option,
[Parameter(ParameterSetName = 'GetHelp')]
[ValidateSet("all", "full")]
[String] $Help
)
$AnswersYesArray = @("yes", "assume-yes", "assumeyes", "answersyes", "answers-yes")
$AnswersUpgradeArray = @("update", "upgrade")
$AnswersCleanupArray = @("cleanup", "deletetempfiles", "deletetemp")
$Identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$Principal = New-Object Security.Principal.WindowsPrincipal $Identity
$IsAdmin = $Principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
function EmptyLine() {
Write-Host
}
<# -------------------------------------------------------- #>
<# Checking #>
<# -------------------------------------------------------- #>
function NotAdminMessage() {
<#
.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
This function is not meant to run independently
#>
EmptyLine
Write-Host "Please run this script as an admin access." -ForegroundColor Red
Write-Host "Because almost all commands require admin access." -ForegroundColor Red
}
<# -------------------------------------------------------- #>
<# Menu or Title #>
<# -------------------------------------------------------- #>
function HelpMenu() {
<#
.SYNOPSIS
Help Menu Function
.DESCRIPTION
Show the help menu for all things that this script can do
.EXAMPLE
.\SystemUpgrade.ps1 -Help all
.EXAMPLE
.\SystemUpgrade.ps1 -Help full
.NOTES
This function is not meant to run independently
#>
Write-Host "This is a Help Command for this script." -ForegroundColor Cyan
Write-Host "Example: .\SystemUpgrade.ps1 [parameter] [the option]"
EmptyLine
Write-Host "[parameter] :" -ForegroundColor Green
Write-Host "> -help -> Display this help message."
Write-Host "> -option -> An option on how you want to run the script."
Write-Host "Example: .\SystemUpgrade.ps1 -help" -ForegroundColor Red
EmptyLine
Write-Host "-option [the option] :" -ForegroundColor Green
Write-Host "> yes -> Automatically answers yes to every questions."
Write-Host "> assume-yes -> Automatically answers yes to every questions."
Write-Host "> assumeyes -> Automatically answers yes to every questions."
Write-Host "> answersyes -> Automatically answers yes to every questions."
Write-Host "> answers-yes -> Automatically answers yes to every questions."
Write-Host "> half-yes -> Answers yes to all questions except for winget."
Write-Host "> upgrade -> Answers yes to all questions for upgrades only."
Write-Host "> update -> Answers yes to all questions for upgrades only."
Write-Host "> cleanup -> Answers yes to delete temporary files script."
Write-Host "> deletetempfiles -> Answers yes to delete temporary files script."
Write-Host "> deletetemp -> Answers yes to delete temporary files script."
Write-Host "> normal -> Run the script normally."
Write-Host "> regular -> Run the script normally."
Write-Host "Example: .\SystemUpgrade.ps1 -option yes" -ForegroundColor Red
Write-Host "Example: .\SystemUpgrade.ps1 -option normal" -ForegroundColor Red
EmptyLine
Write-Host "-help [the option] :" -ForegroundColor Green
Write-Host "> all"
Write-Host "> full"
Write-Host "Example: .\SystemUpgrade.ps1 -help all" -ForegroundColor Red
}
function Title() {
<#
.SYNOPSIS
Shows the script title at the start of the script
.NOTES
This function is not meant to run independently
#>
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
.NOTES
This function is not meant to run independently
#>
Write-Host "
__| |_________________________________| |__
(__| |_________________________________| |__)
| | End Script | |
__| |_________________________________| |__
(__|_|_________________________________|_|__) " -ForegroundColor Magenta
}
<# -------------------------------------------------------- #>
<# Script Function #>
<# -------------------------------------------------------- #>
function UpdatePowershellModule() {
<#
.SYNOPSIS
Update PowerShell Module
.DESCRIPTION
This function updates all installed PowerShell modules if it needs to.
.NOTES
This function is not meant to run independently
#>
if (!(Get-Command -Name Update-Module)) {
Write-Host "There's no Update-Module command. So the script will skip this process." -ForegroundColor Blue
return
}
function RunUpdateModule() {
<#
.SYNOPSIS
Main code function
.DESCRIPTION
This function consist of what actual commands that get run.
.NOTES
This function is not meant to run independently.
This function is always run if the user chose to run outer function.
#>
Write-Host "Checking update for all PowerShell modules..." -ForegroundColor Yellow
if (Get-Command -Name pwsh) {
# automatically answers yes to all prompt, powershell V7 is installed
Write-Output A | pwsh -c Update-Module
}
else {
Update-Module -AcceptLicense
}
}
# user add option to automatically answers yes or half yes or upgrade only
if (($AnswersYesArray -Contains $Option) -or ($Option -eq "half-yes") -or ($AnswersUpgradeArray -Contains $Option)) {
RunUpdateModule
return
} # user use -Option cleanup
elseif ($AnswersCleanupArray -Contains $Option) {
return
}
Write-Host "Update all PowerShell module ? [Y/n] " -ForegroundColor Blue -NoNewline
$UpdateModuleOption = Read-Host
if (($UpdateModuleOption.ToLower() -eq "y") -or ($UpdateModuleOption -eq "")) {
RunUpdateModule
}
else {
EmptyLine
Write-Host "Skipping PowerShell update module..." -ForegroundColor Yellow
return
}
}
function WindowsUpdateScript() {
<#
.SYNOPSIS
Windows Update Script
.DESCRIPTION
This function checks for Windows Update.
.NOTES
This function is not meant to run independently.
#>
if (!(Get-Module -Name "PSWindowsUpdate" -ListAvailable) -and !(Get-Command -Name Get-WindowsUpdate)) { return }
# main code function
function RunWindowsUpdate() {
<#
.SYNOPSIS
Main code function
.DESCRIPTION
This function consist of what actual commands that get run.
.NOTES
This function is not meant to run independently.
This function is always run if the user chose to run outer function.
#>
do {
Clear-Host
Write-Host "Checking Windows Update..." -ForegroundColor Yellow
Get-WindowsUpdate -Verbose
EmptyLine
Write-Host "Type the KB Article ID! Type 'exit' or leave it empty to skip this step!" -ForegroundColor Green
Write-Host "Type 'all' if you want to do all Windows Updates!" -ForegroundColor Green
Write-Host "You can type more than one, just make sure to put a space after each one!" -ForegroundColor Green
Write-Host "Example : KB5026958 KB2267602" -ForegroundColor Red
EmptyLine
$WindowsUpdateChoose = Read-Host -Prompt "KB Article ID "
# user typed in 'all' then do all windows updates
if ($WindowsUpdateChoose.ToLower() -eq "all") {
Install-WindowsUpdate -Verbose -AcceptAll -IgnoreReboot
}
if ((!$WindowsUpdateChoose) -or ($WindowsUpdateChoose.ToLower() -eq 'exit')) {
EmptyLine
Write-Host "Exiting Windows Update..." -ForegroundColor Yellow
break
}
else {
$ArrayID = $WindowsUpdateChoose.Split(" ")
Clear-Host
foreach ($KBArticleID in $ArrayID) {
Write-Host "Downloading Windows Update..." -ForegroundColor Yellow
Get-WindowsUpdate -Verbose -Install -IgnoreReboot -AcceptAll -KBArticleID $KBArticleID
}
}
Start-Sleep 8
Clear-Host
} while ($true)
}
# user add option to automatically answers yes or half yes or upgrade only
if (($AnswersYesArray -Contains $Option) -or ($Option -eq "half-yes") -or ($AnswersUpgradeArray -Contains $Option)) {
EmptyLine
Write-Host "Checking Windows Update..." -ForegroundColor Yellow
Get-WindowsUpdate -Verbose
EmptyLine
Write-Host "Installing Windows Update..." -ForegroundColor Yellow
Install-WindowsUpdate -Verbose -AcceptAll -IgnoreReboot
return
} # user use -Option cleanup
elseif ($AnswersCleanupArray -Contains $Option) {
return
}
Write-Host "Check for Windows Update ? [Y/n] " -ForegroundColor Blue -NoNewline
$WindowsUpdateOption = Read-Host
if (($WindowsUpdateOption.ToLower() -eq "y") -or ($WindowsUpdateOption -eq "")) {
RunWindowsUpdate
}
else {
EmptyLine
Write-Host "Skipping Windows Update..." -ForegroundColor Yellow
return
}
}
function WingetUpdateScript() {
<#
.SYNOPSIS
Winget Upgrade Script
.DESCRIPTION
This function checks for outdated winget applications and upgrades it if the user chose to.
Not only that, it also checks the whole application installed on the system if winget can find the application on winget repository.
.NOTES
This function is not meant to run independently.
This function is always run if the user chose to run outer function.
#>
if (!(Get-Command -Name winget) -and !(Test-Path "$env:LOCALAPPDATA\Microsoft\WindowsApps\winget.exe")) { return }
function RunWingetUpgrade() {
<#
.SYNOPSIS
Main code function
.DESCRIPTION
This function consist of what actual commands that get run.
.NOTES
This function is not meant to run independently.
This function is always run if the user chose to run outer function.
#>
do {
EmptyLine
Write-Host "Checking for upgradable winget applications..." -ForegroundColor Yellow
winget upgrade --include-unknown
EmptyLine
Write-Host "Type the Id! Type 'exit' or leave it empty to skip this step!" -ForegroundColor Green
Write-Host "Type 'all' if you want to upgrade all winget applications!" -ForegroundColor Green
Write-Host "You can type more than one, just make sure to put a space after each one!" -ForegroundColor Green
Write-Host "Example : Zoom.Zoom Git.Git BlenderFoundation.Blender" -ForegroundColor Red
EmptyLine
$WingetUpgradeChoose = Read-Host -Prompt "App Id "
# if user typed in all, then upgrade all winget applications
if ($WingetUpgradeChoose.ToLower() -eq "all") {
winget upgrade --include-unknown --all
}
if ((!$WingetUpgradeChoose) -or ($WingetUpgradeChoose.ToLower() -eq 'exit')) {
EmptyLine
Write-Host "Exiting winget upgrade..." -ForegroundColor Yellow
break
}
else {
# turn id into array
$ArrayID = $WingetUpgradeChoose.Split(" ")
Clear-Host
Write-Host "Application(s) that will be upgraded :"
# each of app name in array of id
foreach ($AppName in $ArrayID) {
# list all installed app, pipe it by its id and only print words after 'Found' and before '[', also replace [ with nothing
$MatchString = ((winget show $AppName | Select-String -Pattern "Found (.*\s[\[])").Matches.Groups[1].Value).Replace("[", "")
Write-Host "- " $MatchString -ForegroundColor Magenta
}
EmptyLine
foreach ($AppID in $ArrayID) {
winget upgrade --include-unknown $AppID
}
}
Start-Sleep 8
Clear-Host
} while ($true)
}
# user add option automatically answers yes or upgrade only
if (($AnswersYesArray -Contains $Option) -or ($AnswersUpgradeArray -Contains $Option)) {
Write-Host "Upgrading all installed applications if available..." -ForegroundColor Yellow
winget upgrade --include-unknown --all
return
} # user use -Option cleanup
elseif ($AnswersCleanupArray -Contains $Option) {
return
}
Write-Host "Run winget upgrade ? [Y/n] " -ForegroundColor Blue -NoNewline
$WingetUpgradeOption = Read-Host
if (($WingetUpgradeOption.Tolower() -eq "y") -or ($WingetUpgradeOption -eq "")) {
RunWingetUpgrade
}
else {
EmptyLine
Write-Host "Skipping application update using winget..." -ForegroundColor Yellow
return
}
}
function ChocolateyUpdateScript() {
<#
.SYNOPSIS
Chocolatey Upgrade Script
.DESCRIPTION
This function checks for outdated Chocolatey applications only and
updates it if the user chose to.
.NOTES
This function is not meant to run independently.
#>
if (!(Get-Command -Name choco) -and !(Test-Path "$env:ChocolateyInstall\choco.exe")) { return }
function RunChocoUgrade() {
<#
.SYNOPSIS
Main code function
.DESCRIPTION
This function consist of what actual commands that get run.
.NOTES
This function is not meant to run independently.
This function is always run if the user chose to run outer function.
#>
do {
choco outdated
EmptyLine
Write-Host "Type the package name! Type 'exit' or leave it empty to skip this step!" -ForegroundColor Green
Write-Host "Type 'all' if you want to upgrade all packages!" -ForegroundColor Green
Write-Host "You can type more than one, just make sure to put a space after each one!" -ForegroundColor Green
Write-Host "Example : python hwinfo chocolatey" -ForegroundColor Red
EmptyLine
$ChocoUpgradeChoose = Read-Host -Prompt "App Name "
if ((!$ChocoUpgradeChoose) -or ($ChocoUpgradeChoose.ToLower() -eq 'exit')) {
EmptyLine
Write-Host "Exiting choco upgrade..." -ForegroundColor Yellow
break
}
else {
Clear-Host
Write-Host "Your Choice : " -NoNewline
Write-Host "$ChocoUpgradeChoose" -ForegroundColor Magenta
EmptyLine
# if you answers all, then just upgrade all
if ($ChocoUpgradeChoose.ToLower() -eq "all") {
choco upgrade --yes all
}
$Array = $ChocoUpgradeChoose.Split(" ")
foreach ($App in $Array) {
choco upgrade --yes $App
}
}
Start-Sleep 8
Clear-Host
} while ($true)
}
# user add option automatically answers yes or half yes or upgrade only
if (($AnswersYesArray -Contains $Option) -or ($Option -eq "half-yes") -or ($AnswersUpgradeArray -Contains $Option)) {
choco outdated
Write-Host "Updating all chocolatey application(s)..." -ForegroundColor Yellow
choco upgrade --yes all
return
} # user use -Option cleanup
elseif ($AnswersCleanupArray -Contains $Option) {
return
}
Write-Host "Run choco upgrade ? [Y/n] " -ForegroundColor Blue -NoNewline
$ChocoUpgradeOption = Read-Host
if (($ChocoUpgradeOption.ToLower() -eq "y") -or ($ChocoUpgradeOption -eq "")) {
RunChocoUgrade
}
else {
EmptyLine
Write-Host "Skipping application update using Chocolatey..." -ForegroundColor Yellow
return
}
}
function ScanSystemCorruptionFiles() {
<#
.SYNOPSIS
System Corruption Scan
.DESCRIPTION
This function scans for system corruption files and tries to fix it.
.NOTES
This function is not meant to run independently.
#>
function RunScan() {
<#
.SYNOPSIS
Main code function
.DESCRIPTION
This function consist of what actual commands that get run.
.NOTES
This function is not meant to run independently.
This function is always run if the user chose to run outer function.
#>
Write-Host "`n(1/4) Run 'chkdsk' (check disk)" -ForegroundColor Yellow
chkdsk /scan
Write-Host "`n(2/4) Run 'sfc /SCANNOW' (System File Checker) - 1st scan" -ForegroundColor Yellow
sfc /SCANNOW
Write-Host "`n(3/4) Run DISM (Deployment Image Servicing and Management tool)" -ForegroundColor Yellow
DISM /Online /Cleanup-Image /Restorehealth
Write-Host "`n(4/4) Run 'sfc /SCANNOW' (System File Checker) - 2nd scan" -ForegroundColor Yellow
sfc /SCANNOW
}
# user add option automatically answers yes or half yes
if (($AnswersYesArray -Contains $Option) -or ($Option -eq "half-yes")) {
RunScan
return
} # user use -Option upgrade or -Option cleanup
elseif ($AnswersUpgradeArray -Contains $Option -or ($AnswersCleanupArray -Contains $Option)) {
return
}
Write-Host "Check for system corruption files ? [Y/n] " -ForegroundColor Blue -NoNewline
$ScanCorruptionFilesOption = Read-Host
if (($ScanCorruptionFilesOption.ToLower() -eq "y") -or ($ScanCorruptionFilesOption -eq "")) {
RunScan
}
else {
EmptyLine
Write-Host "Skipping scan for system corruption files..." -ForegroundColor Yellow
return
}
}
function SystemCleanup {
<#
.SYNOPSIS
System Cleanup
.DESCRIPTION
This function delete all temporary files and old Windows Update.
Also delete all unnesessary files and browser cache.
It is using the Disk Clean-up tool.
.NOTES
This function is not meant to run independently.
#>
function RunDiskCleanUp() {
<#
.SYNOPSIS
Run Disk Clean-up Utility
.DESCRIPTION
Run Disk Clean-up.
And exit this function if it doesn't find Disk Clean-up tool.
.NOTES
This function is not meant to run independently.
#>
if ((Get-Command -Name cleanmgr) -and (Test-Path "$env:windir\system32\cleanmgr.exe")) {
EmptyLine
Write-Host "Running Disk Cleanup..." -ForegroundColor Yellow
cleanmgr.exe /d $env:HOMEDRIVE /VERYLOWDISK
}
else {
EmptyLine
Write-Host "'cleanmgr' DOES NOT exist as a command." -ForegroundColor Red
Write-Host "Skipping this process.."
return
}
}
function DeleteTempFiles() {
<#
.SYNOPSIS
Delete Temporary Files
.DESCRIPTION
Delete all temporary files and folders in Windows Temp directory.
And exits if it doesn't find that directory.
.NOTES
This function is not meant to run independently.
#>
if ((Test-Path "$env:windir\Temp") -and (Test-Path "$env:TEMP")) {
if (!(Get-ChildItem -Path "$env:windir\Temp" | Write-Output) -and !(Get-ChildItem -Path "$env:TEMP" | Write-Output)) {
EmptyLine
Write-Host "No need to, temporary folders are already empty. 😁👍" -ForegroundColor Yellow
return
}
EmptyLine
Write-Host "Deleting Temporary Files..." -ForegroundColor Yellow
Get-ChildItem -Path "$env:windir\Temp" *.* -Recurse | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
Get-ChildItem -Path $env:TEMP *.* -Recurse | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
}
else {
EmptyLine
Write-Host "$env:windir\Temp and temporary folder in environment variable DOES NOT exist." -ForegroundColor Red
Write-Host "Skipping this process..."
return
}
}
function EmptyRecycleBin() {
<#
.SYNOPSIS
Empty Recycle Bin
.DESCRIPTION
Delete all files and folders in the recycle bin.
If it doesn't find the recycle bin, or if anything goes wrong,
just exit this function
.NOTES
This function is not meant to run independently.
#>
if (Get-Command -Name Clear-RecycleBin) {
EmptyLine
Write-Host "Deleting contents inside recycle bin..." -ForegroundColor Yellow
Clear-RecycleBin -Force
}
elseif (Test-Path $env:RecycleBin -ErrorAction Stop) {
EmptyLine
Write-Host "Path found for recycle bin. Deleting contents inside..." -ForegroundColor Yellow
Remove-Item -Path $env:RecycleBin -Recurse -Force
}
else {
EmptyLine
Write-Host "Neither works, 'Clear-RecycleBin' command DOES NOT exist and CANNOT find path to recycle bin." -ForegroundColor Red
return
}
}
# user add option automatically answers yes or half yes
if (($AnswersYesArray -Contains $Option) -or ($Option -eq "half-yes") -or ($AnswersCleanupArray -Contains $Option)) {
RunDiskCleanUp
DeleteTempFiles
EmptyRecycleBin
return
} # user add option upgrade skip this function
elseif ($AnswersUpgradeArray -Contains $Option) {
return
}
Write-Host "Delete unused files and folders ? [Y/n] " -ForegroundColor Blue -NoNewline
$RunSystemCleanupOption = Read-Host
if (($RunSystemCleanupOption.ToLower() -eq "y") -or ($RunSystemCleanupOption -eq "")) {
RunDiskCleanUp
DeleteTempFiles
EmptyRecycleBin
}
else {
EmptyLine
Write-Host "Skipping System Cleanup..." -ForegroundColor Yellow
return
}
}
<# -------------------------------------------------------- #>
<# Install Prerequisite Package or Module #>
<# -------------------------------------------------------- #>
function WindowsUpdateModuleInstall() {
<#
.SYNOPSIS
PSWindowsUpdate PowerShell Module Install
.DESCRIPTION
Install Windows Update PowerShell module to be able to update windows from the terminal.
It will install the script if the user chose to do so.
.NOTES
This function is not meant to run independently.
This function only going to run if PSWindowsUpdate module is not installed.
#>
Write-Host "PSWindowsUpdate Module is not installed in this system." -ForegroundColor Red
Write-Host "PSWindowsUpdate is a PowerShell module that can install Windows Update through terminal." -ForegroundColor Green
Write-Host "Do you want to install 'PSWindowsUpdate' module ? [Y/n] " -ForegroundColor Cyan -NoNewline
$PSWUInstallOption = Read-Host
if (($PSWUInstallOption.ToLower() -eq "y") -or $PSWUInstallOption -eq "") {
Install-Module -Name PSWindowsUpdate
}
else {
EmptyLine
Write-Host "Skipping PSWindowsUpdate module install..." -ForegroundColor Yellow
return
}
}
function WingetInstall() {
<#
.SYNOPSIS
Install Winget
.DESCRIPTION
Install Winget to be able to update all applications from the terminal.
It will install the script if the user chose to do so.
It installs the winget-install PowerShell module and running the winget-install.ps1 script.
.NOTES
This function is not meant to run independently.
This function only going to run if winget is not installed.
#>
Write-Host "winget is not installed in this system." -ForegroundColor Red
Write-Host "winget is a Windows Package Manager that enables installing applications through terminal." -ForegroundColor Cyan
Write-Host "Do you want to install 'winget' package manager ? [Y/n] " -ForegroundColor Cyan -NoNewline
$WingetInstallOption = Read-Host
if (($WingetInstallOption.ToLower() -eq "y") -or ($WingetInstallOption -eq "")) {
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
Install-Script -Name winget-install -Force
winget-install.ps1
}
else {
EmptyLine
Write-Host "Skip installing winget package manager..." -ForegroundColor Yellow
return
}
}
function ChocolateyInstall() {
<#
.SYNOPSIS
Chocolatey Install
.DESCRIPTION
Install Chocolatey to be able to update and install applications from the terminal.
It is doing it by downloading an install script from the official website and running it.
It will install the script if the user chose to do so.
.NOTES
This function is not meant to run independently.
This function only going to run if chocolatey is not installed.
#>
Write-Host "Chocolatey is not installed in this system." -ForegroundColor Red
Write-Host "Chocolatey is a universal package manager for windows that enables installing applications through terminal." -ForegroundColor Cyan
Write-Host "Do you want to install 'Chocolatey' package manager ? [Y/n] " -ForegroundColor Cyan -NoNewline
$ChocolateyInstallOption = Read-Host
if (($ChocolateyInstallOption.ToLower() -eq "y") -or ($ChocolateyInstallOption -eq "")) {
Set-ExecutionPolicy Bypass -Scope Process -Force
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) -ErrorAction Stop
}
else {
EmptyLine
Write-Host "Skip installing Chocolatey package manager..." -ForegroundColor Yellow
return
}
}
<# -------------------------------------------------------- #>
<# DevTools #>
<# -------------------------------------------------------- #>
function PipPackageUpgrade() {
<#
.SYNOPSIS
Upgrade Pip Packages
.DESCRIPTION
List outdated pip packages and user can choose which package to upgrade or
to upgrade all outdated pip packages
.NOTES
This function is not meant to run independently
This function only going to run if python is installed.
#>
function RunPipUpgrade() {
<#
.SYNOPSIS
Main code function
.DESCRIPTION
This function consist of what actual commands that get run.
.NOTES
This function is not meant to run independently.
This function is always run if the user chose to run outer function.
#>
do {
EmptyLine
Write-Host "List outdated pip packages..." -ForegroundColor Yellow
pip list --outdated
EmptyLine
Write-Host "Type the package name! Type 'exit' or leave it empty to skip this step!" -ForegroundColor Green
Write-Host "Type 'all' if you want to upgrade all packages!" -ForegroundColor Green
Write-Host "You can type more than one, just make sure to put a space after each one!" -ForegroundColor Green
Write-Host "Example : python hwinfo chocolatey" -ForegroundColor Red
EmptyLine
$pipUpgradeChoose = Read-Host -Prompt "Package name "
if ($pipUpgradeChoose.ToLower() -eq "all") {
Clear-Host
python.exe -m pip install --upgrade pip
$packages = pip list --outdated | Select-Object -Skip 2 | ForEach-Object {
($_ -split "\s+")[0].Trim()
}
$Array = $packages -Split " "
foreach ($package in $Array) {
pip install --upgrade $package
}
}
elseif (!$pipUpgradeChoose -or $pipUpgradeChoose.ToLower() -eq "exit") {
EmptyLine
Write-Host "Exiting pip upgrade..." -ForegroundColor Yellow
break
}
else {
$Array = $pipUpgradeChoose.Split(" ")
foreach ($package in $Array) {
pip install --upgrade $package
}
}
Start-Sleep 8
Clear-Host
} while ($true)
}
# user add option automatically answers yes or half yes or upgrade only
if (($AnswersYesArray -Contains $Option) -or ($Option -eq "half-yes") -or ($AnswersUpgradeArray -Contains $Option)) {
choco outdated
Write-Host "Updating all pip package(s)..." -ForegroundColor Yellow
pip list --format freeze | ForEach-Object { pip install --upgrade $_.split('==')[0] }
return
} # user use -Option cleanup
elseif ($AnswersCleanupArray -Contains $Option) {
return
}
EmptyLine
Write-Host "Run pip upgrade ? [Y/n] " -ForegroundColor Blue -NoNewline
$PipUpgradeOption = Read-Host
if (($PipUpgradeOption.ToLower() -eq "y") -or ($PipUpgradeOption -eq "")) {
RunPipUpgrade
}
else {
EmptyLine
Write-Host "Skipping pip upgrade..." -ForegroundColor Yellow
return
}
}
<####################################################################>
<# Main Function #>
<####################################################################>
function Main() {
<#
.SYNOPSIS
Main Function
.DESCRIPTION
Main function that will execute all functions created.
.NOTES
This function is not meant to run indepently.
#>
if (!$IsAdmin) {
# if run script not as admin
NotAdminMessage
return
}
if ($Help) {
# if user use -Help
HelpMenu
return
}
Title
EmptyLine
UpdatePowershellModule
<# -------------------------------------------------------- #>
<# Checking Prerequisite #>
<# -------------------------------------------------------- #>
EmptyLine
# install PSWindowsUpdate module if isn't already
if (!(Get-Module -Name "PSWindowsUpdate" -ListAvailable)) {
WindowsUpdateModuleInstall
}
else {
WindowsUpdateScript
}
EmptyLine
# install winget if it isn't already
if (!(Get-Command -Name winget) -and !(Test-Path "$env:LOCALAPPDATA\Microsoft\WindowsApps\winget.exe")) {
WingetInstall
}
else {
WingetUpdateScript
}
EmptyLine
# install chocolatey if isn't already
if (!(Get-Command -Name choco) -and !(Test-Path "$env:ChocolateyInstall\choco.exe")) {
ChocolateyInstall
}
else {
ChocolateyUpdateScript
}
# if python is installed, run update pip
if ((Get-Command -Name python) -and (Test-Path "$env:LOCALAPPDATA\Programs\Python")) {
PipPackageUpgrade
}
EmptyLine
ScanSystemCorruptionFiles
EmptyLine
SystemCleanup