-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
winget-install.ps1
1269 lines (1040 loc) · 53.7 KB
/
winget-install.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
<#PSScriptInfo
.VERSION 5.0.5
.GUID 3b581edb-5d90-4fa1-ba15-4f2377275463
.AUTHOR asheroto
.COMPANYNAME asheroto
.TAGS PowerShell Windows winget win get install installer fix script setup
.PROJECTURI https://github.com/asheroto/winget-install
.RELEASENOTES
[Version 0.0.1] - Initial Release.
[Version 0.0.2] - Implemented function to get the latest version of winget and its license.
[Version 0.0.3] - Signed file for PSGallery.
[Version 0.0.4] - Changed URI to grab latest release instead of releases and preleases.
[Version 0.0.5] - Updated version number of dependencies.
[Version 1.0.0] - Major refactor code, see release notes for more information.
[Version 1.0.1] - Fixed minor bug where version 2.8 was hardcoded in URL.
[Version 1.0.2] - Hardcoded UI Xaml version 2.8.4 as a failsafe in case the API fails. Added CheckForUpdates, Version, Help functions. Various bug fixes.
[Version 1.0.3] - Added error message to catch block. Fixed bug where appx package was not being installed.
[Version 1.0.4] - MisterZeus optimized code for readability.
[Version 2.0.0] - Major refactor. Reverted to UI.Xaml 2.7.3 for stability. Adjusted script to fix install issues due to winget changes (thank you ChrisTitusTech). Added in all architecture support.
[Version 2.0.1] - Renamed repo and URL references from winget-installer to winget-install. Added extra space after the last line of output.
[Version 2.0.2] - Adjusted CheckForUpdates to include Install-Script instructions and extra spacing.
[Version 2.1.0] - Added alternate method/URL for dependencies in case the main URL is down. Fixed licensing issue when winget is installed on Server 2022.
[Version 2.1.1] - Switched primary/alternate methods. Added Cleanup function to avoid errors when cleaning up temp files. Added output of URL for alternate method. Suppressed Add-AppxProvisionedPackage output. Improved success message. Improved verbiage. Improve PS script comments. Added check if the URL is empty. Moved display of URL beneath the check.
[Version 3.0.0] - Major changes. Added OS version detection checks - detects OS version, release ID, ensures compatibility. Forces older file installation for Server 2022 to avoid issues after installing. Added DebugMode, DisableCleanup, Force. Renamed CheckForUpdates to CheckForUpdate. Improved output. Improved error handling. Improved comments. Improved code readability. Moved CheckForUpdate into function. Added PowerShellGalleryName. Renamed Get-OSVersion to Get-OSInfo. Moved architecture detection into Get-OSInfo. Renamed Get-NewestLink to Get-WingetDownloadUrl. Have Get-WingetDownloadUrl not get preview releases.
[Version 3.0.1] - Updated Get-OSInfo function to fix issues when used on non-English systems. Improved error handling of "resources in use" error.
[Version 3.0.2] - Added winget registration command for Windows 10 machines.
[Version 3.1.0] - Added support for one-line installation with irm and iex compatible with $Force session variable. Added UpdateSelf command to automatically update the script to the latest version. Created short URL asheroto.com/winget.
[Version 3.1.1] - Changed winget register command to run on all OS versions.
[Version 3.2.0] - Added -ForceClose logic to relaunch the script in conhost.exe and automatically end active processes associated with winget that could interfere with the installation. Improved verbiage on winget already installed.
[Version 3.2.1] - Fixed minor glitch when using -Version or -Help parameters.
[Version 3.2.2] - Improved script exit functionality.
[Version 3.2.3] - Improved -ForceClose window handling with x86 PowerShell process.
[Version 3.2.4] - Improved verbiage for incompatible systems. Added importing Appx module on Windows Server with PowerShell 7+ systems to avoid error message.
[Version 3.2.5] - Removed pause after script completion. Added optional Wait parameter to force script to wait several seconds for script output.
[Version 3.2.6] - Improved ExitWithDelay function. Sometimes PowerShell will close the window accidentally, even when using the proper 'exit' command. Adjusted several closures for improved readability. Improved error code checking. Fixed glitch with -Wait param.
[Version 3.2.7] - Addded ability to install for all users. Added checks for Windows Sandbox and administrative privileges.
[Version 4.0.0] - Microsoft created some short URLs for winget. Removed a large portion of the script to use short URLs instead. Simplified and refactored. Switched debug param from DebugMode to Debug.
[Version 4.0.1] - Fixed PowerShell help information.
[Version 4.0.2] - Adjusted UpdateSelf function to reset PSGallery to original state if it was not trusted. Improved comments.
[Version 4.0.3] - Updated UI.Xaml package as per winget-cli issue #4208.
[Version 4.0.4] - Fixed detection for Windows multi-session.
[Version 4.0.5] - Improved error handling when registering winget.
[Version 4.1.0] - Support for Windows Server 2019 added by installing Visual C++ Redistributable.
[Version 4.1.1] - Minor revisions to comments & debug output.
[Version 4.1.2] - Implemented Visual C++ Redistributable version detection to ensure compatibility with winget.
[Version 4.1.3] - Added additional debug output for Visual C++ Redistributable version detection.
[Version 5.0.0] - Completely changed method to use winget-cli Repair-WingetPackageManager. Added environment path detection and addition if needed. Added NoExit parameter to prevent script from exiting after completion. Adjusted permissions of winget folder path for Server 2019. Improved exit handling to avoid PowerShell window closing.
[Version 5.0.1] - Fixed typo in variable name.
[Version 5.0.2] - Added detection of NuGet and PowerShell version to determine if package provider installation is needed.
[Version 5.0.3] - Fixed missing argument in call to Add-ToEnvironmentPath.
[Version 5.0.4] - Fixed bug with UpdateSelf function. Fixed bug when installing that may cause NuGet prompt to not be suppressed. Introduced Install-NuGetIfRequired function.
[Version 5.0.5] - Fixed exit code issue. Fixes #52.
#>
<#
.SYNOPSIS
Downloads and installs the latest version of winget and its dependencies.
.DESCRIPTION
Downloads and installs the latest version of winget and its dependencies.
This script is designed to be straightforward and easy to use, removing the hassle of manually downloading, installing, and configuring winget. This function should be run with administrative privileges.
.EXAMPLE
winget-install
.PARAMETER Debug
Enables debug mode, which shows additional information for debugging.
.PARAMETER Force
Ensures installation of winget and its dependencies, even if already present.
.PARAMETER ForceClose
Relaunches the script in conhost.exe and automatically ends active processes associated with winget that could interfere with the installation.
.PARAMETER Wait
Forces the script to wait several seconds before exiting.
.PARAMETER NoExit
Forces the script to wait indefinitely before exiting.
.PARAMETER UpdateSelf
Updates the script to the latest version on PSGallery.
.PARAMETER CheckForUpdate
Checks if there is an update available for the script.
.PARAMETER Version
Displays the version of the script.
.PARAMETER Help
Displays the full help information for the script.
.NOTES
Version : 5.0.5
Created by : asheroto
.LINK
Project Site: https://github.com/asheroto/winget-install
#>
[CmdletBinding()]
param (
[switch]$Force,
[switch]$ForceClose,
[switch]$CheckForUpdate,
[switch]$Wait,
[switch]$NoExit,
[switch]$UpdateSelf,
[switch]$Version,
[switch]$Help
)
# Script information
$CurrentVersion = '5.0.5'
$RepoOwner = 'asheroto'
$RepoName = 'winget-install'
$PowerShellGalleryName = 'winget-install'
# Preferences
$ProgressPreference = 'SilentlyContinue' # Suppress progress bar (makes downloading super fast)
$ConfirmPreference = 'None' # Suppress confirmation prompts
# Display version if -Version is specified
if ($Version.IsPresent) {
$CurrentVersion
exit 0
}
# Display full help if -Help is specified
if ($Help) {
Get-Help -Name $MyInvocation.MyCommand.Source -Full
exit 0
}
# Display $PSVersionTable and Get-Host if -Verbose is specified
if ($PSBoundParameters.ContainsKey('Verbose') -and $PSBoundParameters['Verbose']) {
$PSVersionTable
Get-Host
}
# Set debug preferences if -Debug is specified
if ($PSBoundParameters.ContainsKey('Debug') -and $PSBoundParameters['Debug']) {
$DebugPreference = 'Continue'
$ConfirmPreference = 'None'
}
function Get-OSInfo {
<#
.SYNOPSIS
Retrieves detailed information about the operating system version and architecture.
.DESCRIPTION
This function queries both the Windows registry and the Win32_OperatingSystem class to gather comprehensive information about the operating system. It returns details such as the release ID, display version, name, type (Workstation/Server), numeric version, edition ID, version (object that includes major, minor, and build numbers), and architecture (OS architecture, not processor architecture).
.EXAMPLE
Get-OSInfo
This example retrieves the OS version details of the current system and returns an object with properties like ReleaseId, DisplayVersion, Name, Type, NumericVersion, EditionId, Version, and Architecture.
.EXAMPLE
(Get-OSInfo).Version.Major
This example retrieves the major version number of the operating system. The Get-OSInfo function returns an object with a Version property, which itself is an object containing Major, Minor, and Build properties. You can access these sub-properties using dot notation.
.EXAMPLE
$osDetails = Get-OSInfo
Write-Output "OS Name: $($osDetails.Name)"
Write-Output "OS Type: $($osDetails.Type)"
Write-Output "OS Architecture: $($osDetails.Architecture)"
This example stores the result of Get-OSInfo in a variable and then accesses various properties to print details about the operating system.
#>
[CmdletBinding()]
param ()
try {
# Get registry values
$registryValues = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
$releaseIdValue = $registryValues.ReleaseId
$displayVersionValue = $registryValues.DisplayVersion
$nameValue = $registryValues.ProductName
$editionIdValue = $registryValues.EditionId
# Strip out "Server" from the $editionIdValue if it exists
$editionIdValue = $editionIdValue -replace "Server", ""
# Get OS details using Get-CimInstance because the registry key for Name is not always correct with Windows 11
$osDetails = Get-CimInstance -ClassName Win32_OperatingSystem
$nameValue = $osDetails.Caption
# Get architecture details of the OS (not the processor)
# Get only the numbers
$architecture = ($osDetails.OSArchitecture -replace "[^\d]").Trim()
# If 32-bit or 64-bit replace with x32 and x64
if ($architecture -eq "32") {
$architecture = "x32"
} elseif ($architecture -eq "64") {
$architecture = "x64"
}
# Get OS version details (as version object)
$versionValue = [System.Environment]::OSVersion.Version
# Determine product type
# Reference: https://learn.microsoft.com/en-us/dotnet/api/microsoft.powershell.commands.producttype?view=powershellsdk-1.1.0
if ($osDetails.ProductType -eq 1) {
$typeValue = "Workstation"
} elseif ($osDetails.ProductType -eq 2 -or $osDetails.ProductType -eq 3) {
$typeValue = "Server"
} else {
$typeValue = "Unknown"
}
# Extract numerical value from Name
$numericVersion = ($nameValue -replace "[^\d]").Trim()
# If the numeric version is 10 or above, and the caption contains "multi-session", consider it a workstation
if ($numericVersion -ge 10 -and $osDetails.Caption -match "multi-session") {
$typeValue = "Workstation"
}
# Create and return custom object with the required properties
$result = [PSCustomObject]@{
ReleaseId = $releaseIdValue
DisplayVersion = $displayVersionValue
Name = $nameValue
Type = $typeValue
NumericVersion = $numericVersion
EditionId = $editionIdValue
Version = $versionValue
Architecture = $architecture
}
return $result
} catch {
Write-Error "Unable to get OS version details.`nError: $_"
ExitWithDelay 1
}
}
function Get-GitHubRelease {
<#
.SYNOPSIS
Fetches the latest release information of a GitHub repository.
.DESCRIPTION
This function uses the GitHub API to get information about the latest release of a specified repository, including its version and the date it was published.
.PARAMETER Owner
The GitHub username of the repository owner.
.PARAMETER Repo
The name of the repository.
.EXAMPLE
Get-GitHubRelease -Owner "asheroto" -Repo "winget-install"
This command retrieves the latest release version and published datetime of the winget-install repository owned by asheroto.
#>
[CmdletBinding()]
param (
[string]$Owner,
[string]$Repo
)
try {
$url = "https://api.github.com/repos/$Owner/$Repo/releases/latest"
$response = Invoke-RestMethod -Uri $url -ErrorAction Stop
$latestVersion = $response.tag_name
$publishedAt = $response.published_at
# Convert UTC time string to local time
$UtcDateTime = [DateTime]::Parse($publishedAt, [System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::RoundtripKind)
$PublishedLocalDateTime = $UtcDateTime.ToLocalTime()
[PSCustomObject]@{
LatestVersion = $latestVersion
PublishedDateTime = $PublishedLocalDateTime
}
} catch {
Write-Error "Unable to check for updates.`nError: $_"
exit 1
}
}
function CheckForUpdate {
param (
[string]$RepoOwner,
[string]$RepoName,
[version]$CurrentVersion,
[string]$PowerShellGalleryName
)
$Data = Get-GitHubRelease -Owner $RepoOwner -Repo $RepoName
Write-Output ""
Write-Output ("Repository: {0,-40}" -f "https://github.com/$RepoOwner/$RepoName")
Write-Output ("Current Version: {0,-40}" -f $CurrentVersion)
Write-Output ("Latest Version: {0,-40}" -f $Data.LatestVersion)
Write-Output ("Published at: {0,-40}" -f $Data.PublishedDateTime)
if ($Data.LatestVersion -gt $CurrentVersion) {
Write-Output ("Status: {0,-40}" -f "A new version is available.")
Write-Output "`nOptions to update:"
Write-Output "- Download latest release: https://github.com/$RepoOwner/$RepoName/releases"
if ($PowerShellGalleryName) {
Write-Output "- Run: $RepoName -UpdateSelf"
Write-Output "- Run: Install-Script $PowerShellGalleryName -Force"
}
} else {
Write-Output ("Status: {0,-40}" -f "Up to date.")
}
exit 0
}
function UpdateSelf {
try {
# Get PSGallery version of script
$psGalleryScriptVersion = (Find-Script -Name $PowerShellGalleryName).Version
# If the current version is less than the PSGallery version, update the script
if ($CurrentVersion -lt $psGalleryScriptVersion) {
Write-Output "Updating script to version $psGalleryScriptVersion..."
# Check if running in PowerShell 7 or greater
Write-Debug "Checking if NuGet PackageProvider is already installed..."
Install-NuGetIfRequired
# Trust the PSGallery if not already trusted
$psRepoInstallationPolicy = (Get-PSRepository -Name 'PSGallery').InstallationPolicy
if ($psRepoInstallationPolicy -ne 'Trusted') {
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted | Out-Null
}
# Update the script
Install-Script $PowerShellGalleryName -Force
# If PSGallery was not trusted, reset it to its original state
if ($psRepoInstallationPolicy -ne 'Trusted') {
Set-PSRepository -Name 'PSGallery' -InstallationPolicy $psRepoInstallationPolicy | Out-Null
}
Write-Output "Script updated to version $psGalleryScriptVersion."
exit 0
} else {
Write-Output "Script is already up to date."
exit 0
}
} catch {
Write-Output "An error occurred: $_"
exit 1
}
}
function Write-Section($text) {
<#
.SYNOPSIS
Prints a text block surrounded by a section divider for enhanced output readability.
.DESCRIPTION
This function takes a string input and prints it to the console, surrounded by a section divider made of hash characters.
It is designed to enhance the readability of console output.
.PARAMETER text
The text to be printed within the section divider.
.EXAMPLE
Write-Section "Downloading Files..."
This command prints the text "Downloading Files..." surrounded by a section divider.
#>
Write-Output ""
Write-Output ("#" * ($text.Length + 4))
Write-Output "# $text #"
Write-Output ("#" * ($text.Length + 4))
Write-Output ""
}
function Get-WingetDownloadUrl {
<#
.SYNOPSIS
Retrieves the download URL of the latest release asset that matches a specified pattern from the GitHub repository.
.DESCRIPTION
This function uses the GitHub API to get information about the latest release of the winget-cli repository.
It then retrieves the download URL for the release asset that matches a specified pattern.
.PARAMETER Match
The pattern to match in the asset names.
.EXAMPLE
Get-WingetDownloadUrl "msixbundle"
This command retrieves the download URL for the latest release asset with a name that contains "msixbundle".
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$Match
)
$uri = "https://api.github.com/repos/microsoft/winget-cli/releases"
$releases = Invoke-RestMethod -uri $uri -Method Get -ErrorAction Stop
Write-Debug "Getting latest release..."
foreach ($release in $releases) {
if ($release.name -match "preview") {
continue
}
$data = $release.assets | Where-Object name -Match $Match
if ($data) {
return $data.browser_download_url
}
}
Write-Debug "Falling back to the latest release..."
$latestRelease = $releases | Select-Object -First 1
$data = $latestRelease.assets | Where-Object name -Match $Match
return $data.browser_download_url
}
function Get-WingetStatus {
<#
.SYNOPSIS
Checks if winget is installed.
.DESCRIPTION
This function checks if winget is installed.
.EXAMPLE
Get-WingetStatus
#>
# Check if winget is installed
$winget = Get-Command -Name winget -ErrorAction SilentlyContinue
# If winget is installed, return $true
if ($null -ne $winget -and $winget -notlike '*failed to run*') {
return $true
}
# If winget is not installed, return $false
return $false
}
function Handle-Error {
<#
.SYNOPSIS
Handles common errors that may occur during an installation process.
.DESCRIPTION
This function takes an ErrorRecord object and checks for certain known error codes.
Depending on the error code, it writes appropriate warning messages or throws the error.
.PARAMETER ErrorRecord
The ErrorRecord object that represents the error that was caught. This object contains
information about the error, including the exception that was thrown.
.EXAMPLE
try {
# Some code that may throw an error...
} catch {
Handle-Error $_
}
This example shows how you might use the Handle-Error function in a try-catch block.
If an error occurs in the try block, the catch block catches it and calls Handle-Error,
passing the error (represented by the $_ variable) to the function.
#>
param($ErrorRecord)
# Store current value
$OriginalErrorActionPreference = $ErrorActionPreference
# Set to silently continue
$ErrorActionPreference = 'SilentlyContinue'
# Handle common errors
# Not returning $ErrorRecord on some errors is intentional
if ($ErrorRecord.Exception.Message -match '0x80073D06') {
Write-Warning "Higher version already installed."
Write-Warning "That's okay, continuing..."
} elseif ($ErrorRecord.Exception.Message -match '0x80073CF0') {
Write-Warning "Same version already installed."
Write-Warning "That's okay, continuing..."
} elseif ($ErrorRecord.Exception.Message -match '0x80073D02') {
# Stop execution and return the ErrorRecord so that the calling try/catch block throws the error
Write-Warning "Resources modified are in-use. Try closing Windows Terminal / PowerShell / Command Prompt and try again."
Write-Warning "Windows Terminal sometimes has trouble installing winget. If you are using Windows Terminal and the problem persists, run the script with the -ForceClose parameter which will relaunch the script in conhost.exe and automatically end active processes associated with winget that could interfere with the installation. Please note that using the -ForceClose parameter will close the PowerShell window and could break custom scripts that rely on the current PowerShell session."
return $ErrorRecord
} elseif ($ErrorRecord.Exception.Message -match '0x80073CF3') {
# Prerequisite not detected, tell user to run it again
Write-Warning "Problem with one of the prerequisites."
Write-Warning "Try running the script again which usually fixes the issue. If the problem persists, try running the script with the -ForceClose parameter which will relaunch the script in conhost.exe and automatically end active processes associated with winget that could interfere with the installation. Please note that using the -ForceClose parameter will close the PowerShell window and could break custom scripts that rely on the current PowerShell session."
return $ErrorRecord
} elseif ($ErrorRecord.Exception.Message -match '0x80073CF9') {
Write-Warning "Registering winget failed with error code 0x80073CF9."
Write-Warning "This error usually occurs when using the Local System account to install winget. The SYSTEM account is not officially supported by winget and may not work. See the requirements section of the README. If winget is not working, run the installation script again using an Administrator account."
} elseif ($ErrorRecord.Exception.Message -match 'Unable to connect to the remote server') {
Write-Warning "Cannot connect to the Internet to download the required files."
Write-Warning "Try running the script again and make sure you are connected to the Internet."
Write-Warning "Sometimes the nuget.org server is down, so you may need to try again later."
return $ErrorRecord
} elseif ($ErrorRecord.Exception.Message -match "The remote name could not be resolved") {
Write-Warning "Cannot connect to the Internet to download the required files."
Write-Warning "Try running the script again and make sure you are connected to the Internet."
Write-Warning "Make sure DNS is working correctly on your computer."
} else {
# For other errors, we should stop the execution and return the ErrorRecord so that the calling try/catch block throws the error
return $ErrorRecord
}
# Reset to original value
$ErrorActionPreference = $OriginalErrorActionPreference
}
function Get-CurrentProcess {
<#
.SYNOPSIS
Retrieves the current PowerShell process information.
.DESCRIPTION
The Get-CurrentProcess function identifies the current PowerShell process by temporarily changing the console window title. It then filters the list of running processes to find the one with the matching window title. The function returns a custom object containing the Name and Id of the current process.
.EXAMPLE
PS C:\> $result = Get-CurrentProcess
PS C:\> Write-Output $result
This example demonstrates how to call the Get-CurrentProcess function and store its output in a variable named $result. The output is then displayed using Write-Output.
.NOTES
The function temporarily changes the console window title. Ensure no other scripts or processes are dependent on the window title during execution. The function uses a 1-second sleep to allow time for the window title change to take effect. This may vary based on system performance.
#>
$oldTitle = $host.ui.RawUI.WindowTitle
$tempTitle = ([Guid]::NewGuid())
$host.ui.RawUI.WindowTitle = $tempTitle
Start-Sleep 1
$currentProcess = Get-Process | Where-Object { $_.MainWindowTitle -eq $tempTitle }
$currentProcess = [PSCustomObject]@{
Name = $currentProcess.Name
Id = $currentProcess.Id
}
$host.ui.RawUI.WindowTitle = $oldTitle
return $currentProcess
}
function ExitWithDelay {
<#
.SYNOPSIS
Exits the script with a specified exit code after a specified delay, 10 seconds by default.
.DESCRIPTION
This function takes an exit code as an argument, waits for 10 seconds unless specified, and then exits the script with the given exit code.
.PARAMETER ExitCode
The exit code to use when exiting the script.
.EXAMPLE
ExitWithDelay -ExitCode 1
Waits for 10 seconds (default) and then exits the script with an exit code of 1.
.EXAMPLE
ExitWithDelay -ExitCode 2 -Seconds 5
Waits for 5 seconds and then exits the script with an exit code of 2.
.NOTES
Use this function to introduce a delay before exiting the script, allowing time for any cleanup or logging activities.
#>
param (
[int]$ExitCode,
[int]$Seconds = 10
)
# Debug mode output
if ($Debug -and $Wait) {
Write-Warning "Wait specified, waiting several seconds..."
} elseif ($Debug -and !$Wait) {
Write-Warning "Wait not specified, exiting immediately..."
}
# If Wait is specified, wait for x seconds before exiting
if ($Wait) {
# Waiting for x seconds output
Write-Output "`nWaiting for $Seconds seconds before exiting..."
Start-Sleep -Seconds $Seconds
}
# If NoExit is specified, do not exit the script
if ($NoExit) {
Write-Output "Script completed. Pausing indefinitely. Press any key to exit..."
Read-Host
}
# Exit the script with exit code
if ($MyInvocation.CommandOrigin -eq "Runspace") {
Break
} else {
Exit $ExitCode
}
}
function Import-GlobalVariable {
<#
.SYNOPSIS
This function checks if a specified global variable exists and imports its value into a script scope variable of the same name.
.DESCRIPTION
The Import-GlobalVariable function allows you to specify the name of a variable. It checks if a global variable with that name exists, and if it does, it imports its value into a script scope variable with the same name.
.PARAMETER VariableName
The name of the variable to check and import if it exists in the global scope.
#>
[CmdletBinding()]
param(
[string]$VariableName
)
# Check if the specified global variable exists; if yes, import its value
try {
$globalValue = Get-Variable -Name $VariableName -ValueOnly -Scope Global -ErrorAction Stop
Set-Variable -Name $VariableName -Value $globalValue -Scope Script
} catch {
# If the variable does not exist, do nothing
}
}
function Test-AdminPrivileges {
<#
.SYNOPSIS
Checks if the script is running with Administrator privileges. Returns $true if running with Administrator privileges, $false otherwise.
.DESCRIPTION
This function checks if the current PowerShell session is running with Administrator privileges by examining the role of the current user. It returns $true if the current user is an Administrator, $false otherwise.
.EXAMPLE
Test-AdminPrivileges
.NOTES
This function is particularly useful for scripts that require elevated permissions to run correctly.
#>
if (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
return $true
}
return $false
}
Function New-TemporaryFile2 {
<#
.SYNOPSIS
Creates a new temporary file.
.DESCRIPTION
This function generates a temporary file and returns the full path of the file. The New-TemporaryFile command is not available in PowerShell 5.1 and earlier so this function is a workaround.
.EXAMPLE
$tempFile = New-TemporaryFile2
This example creates a new temporary file and stores its path in the $tempFile variable.
.OUTPUTS
String. The full path of the created temporary file.
#>
# Create a temporary file
$tempPath = [System.IO.Path]::GetTempPath()
$tempFile = [System.IO.Path]::Combine($tempPath, [System.IO.Path]::GetRandomFileName())
$null = New-Item -Path $tempFile -ItemType File -Force
# Return the path of the temporary file
return $tempFile
}
function Path-ExistsInEnvironment {
param (
[string]$PathToCheck,
[string]$Scope = 'Both' # Valid values: 'User', 'System', 'Both'
)
<#
.SYNOPSIS
Checks if the specified path exists in the specified PATH environment variable.
.DESCRIPTION
This function checks if a given path is present in the user, system-wide, or both PATH environment variables.
.PARAMETER PathToCheck
The directory path to check in the environment PATH variable.
.PARAMETER Scope
The scope to check in the environment PATH variable. Valid values are 'User', 'System', or 'Both'. Default is 'Both'.
.EXAMPLE
Path-ExistsInEnvironment -PathToCheck "C:\Program Files\MyApp" -Scope 'User'
#>
$pathExists = $false
if ($Scope -eq 'User' -or $Scope -eq 'Both') {
$userEnvPath = $env:PATH
if (($userEnvPath -split ';').Contains($PathToCheck)) {
$pathExists = $true
}
}
if ($Scope -eq 'System' -or $Scope -eq 'Both') {
$systemEnvPath = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::Machine)
if (($systemEnvPath -split ';').Contains($PathToCheck)) {
$pathExists = $true
}
}
return $pathExists
}
function Add-ToEnvironmentPath {
param (
[Parameter(Mandatory = $true)]
[string]$PathToAdd,
[Parameter(Mandatory = $true)]
[ValidateSet('User', 'System')]
[string]$Scope
)
<#
.SYNOPSIS
Adds the specified path to the environment PATH variable.
.DESCRIPTION
This function adds a given path to the specified scope (user or system) and the current process environment PATH variable if it is not already present.
.PARAMETER PathToAdd
The directory path to add to the environment PATH variable.
.PARAMETER Scope
Specifies whether to add the path to the user or system environment PATH variable.
.EXAMPLE
Add-ToEnvironmentPath -PathToAdd "C:\Program Files\MyApp" -Scope 'System'
#>
# Check if the path is already in the environment PATH variable
if (-not (Path-ExistsInEnvironment -PathToCheck $PathToAdd -Scope $Scope)) {
if ($Scope -eq 'System') {
# Get the current system PATH
$systemEnvPath = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::Machine)
# Add to system PATH
$systemEnvPath += ";$PathToAdd"
[System.Environment]::SetEnvironmentVariable('PATH', $systemEnvPath, [System.EnvironmentVariableTarget]::Machine)
Write-Debug "Adding $PathToAdd to the system PATH."
} elseif ($Scope -eq 'User') {
# Get the current user PATH
$userEnvPath = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::User)
# Add to user PATH
$userEnvPath += ";$PathToAdd"
[System.Environment]::SetEnvironmentVariable('PATH', $userEnvPath, [System.EnvironmentVariableTarget]::User)
Write-Debug "Adding $PathToAdd to the user PATH."
}
# Update the current process environment PATH
if (-not ($env:PATH -split ';').Contains($PathToAdd)) {
$env:PATH += ";$PathToAdd"
Write-Debug "Adding $PathToAdd to the current process environment PATH."
}
} else {
Write-Debug "$PathToAdd is already in the PATH."
}
}
function Set-PathPermissions {
param (
[string]$FolderPath
)
<#
.SYNOPSIS
Grants full control permissions for the Administrators group on the specified directory path.
.DESCRIPTION
This function sets full control permissions for the Administrators group on the specified directory path.
Useful for ensuring that administrators have unrestricted access to a given folder.
.PARAMETER FolderPath
The directory path for which to set full control permissions.
.EXAMPLE
Set-PathPermissions -FolderPath "C:\Program Files\MyApp"
Sets full control permissions for the Administrators group on "C:\Program Files\MyApp".
#>
Write-Debug "Setting full control permissions for the Administrators group on $FolderPath."
# Define the SID for the Administrators group
$administratorsGroupSid = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-544")
$administratorsGroup = $administratorsGroupSid.Translate([System.Security.Principal.NTAccount])
# Retrieve the current ACL for the folder
$acl = Get-Acl -Path $FolderPath
# Define the access rule for full control inheritance
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
$administratorsGroup,
"FullControl",
"ContainerInherit,ObjectInherit",
"None",
"Allow"
)
# Apply the access rule to the ACL and set it on the folder
$acl.SetAccessRule($accessRule)
Set-Acl -Path $FolderPath -AclObject $acl
}
function Test-VCRedistInstalled {
<#
.SYNOPSIS
Checks if Visual C++ Redistributable is installed and verifies the major version is 14.
.DESCRIPTION
This function checks the registry and the file system to determine if Visual C++ Redistributable is installed
and if the major version is 14.
.EXAMPLE
Test-VCRedistInstalled
#>
# Assets
$64BitOS = [System.Environment]::Is64BitOperatingSystem
$64BitProcess = [System.Environment]::Is64BitProcess
# Require running system native process
if ($64BitOS -and -not $64BitProcess) {
Throw 'Please run PowerShell in the system native architecture (x64 PowerShell if x64 Windows).'
}
# Check that uninstall information exists in the registry
$registryPath = [string]::Format(
'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\{0}\Microsoft\VisualStudio\14.0\VC\Runtimes\X{1}',
$(if ($64BitOS -and $64BitProcess) { 'WOW6432Node' } else { '' }),
$(if ($64BitOS) { '64' } else { '86' })
)
$registryExists = Test-Path -Path $registryPath
Write-Debug "Registry Path: $registryPath"
Write-Debug "Registry Exists: $registryExists"
# Check the major version
$majorVersion = if ($registryExists) {
(Get-ItemProperty -Path $registryPath -Name 'Major').Major
} else {
0
}
# Check the full version
$version = if ($registryExists) {
(Get-ItemProperty -Path $registryPath -Name 'Version').Version
} else {
0
}
Write-Debug "Major Version: $majorVersion"
Write-Debug "Version: $version"
# Check that one required DLL exists on the file system
$dllPath = [string]::Format(
'{0}\system32\concrt140.dll',
$env:windir
)
$dllExists = [System.IO.File]::Exists($dllPath)
Write-Debug "DLL Path: $dllPath"
Write-Debug "DLL Exists: $dllExists"
# Determine if VCRedist is installed and major version is 14
return $registryExists -and $majorVersion -eq 14 -and $dllExists
}
function TryRemove {
<#
.SYNOPSIS
Tries to remove a specified file if it exists.
.DESCRIPTION
This function checks if the specified file exists and attempts to remove it.
It will not produce an error if the file does not exist or if the removal fails.
.PARAMETER FilePath
The path to the file that should be removed.
.EXAMPLE
TryRemove -FilePath "C:\path\to\file.txt"
#>
param (
[Parameter(Mandatory = $true)]
[string]$FilePath
)
try {
if (Test-Path -Path $FilePath) {
Remove-Item -Path $FilePath -ErrorAction SilentlyContinue
if ($?) {
Write-Debug "File '$FilePath' was successfully removed."
} else {
Write-Debug "Failed to remove the file '$FilePath'."
}
} else {
Write-Debug "File '$FilePath' does not exist."
}
} catch {
Write-Debug "An error occurred while trying to remove the file '$FilePath'."
}
}
function Install-NuGetIfRequired {
<#
.SYNOPSIS
Checks if the NuGet PackageProvider is installed and installs it if required.
.DESCRIPTION
This function checks whether the NuGet PackageProvider is already installed on the system. If it is not found and the current PowerShell version is less than 7, it attempts to install the NuGet provider using Install-PackageProvider.
For PowerShell 7 or greater, it assumes NuGet is available by default and advises reinstallation if NuGet is missing.
.PARAMETER Debug
Enables debug output for additional details during installation.
.EXAMPLE
Install-NuGetIfRequired
# Checks for the NuGet provider and installs it if necessary.
.NOTES
This function only attempts to install NuGet if the PowerShell version is less than 7.
For PowerShell 7 or greater, NuGet is typically included by default and does not require installation.
#>
# Check if NuGet PackageProvider is already installed, skip package provider installation if found
if (-not (Get-PackageProvider -Name NuGet -ListAvailable -ErrorAction SilentlyContinue)) {
Write-Debug "NuGet PackageProvider not found."
# Check if running in PowerShell version less than 7
if ($PSVersionTable.PSVersion.Major -lt 7) {
# Install NuGet PackageProvider if running PowerShell version less than 7
# PowerShell 7 has limited support for installing package providers, but NuGet is available by default in PowerShell 7 so installation is not required
Write-Debug "Installing NuGet PackageProvider..."
if ($Debug) {
try { Install-PackageProvider -Name "NuGet" -Force -ForceBootstrap -ErrorAction SilentlyContinue } catch { }
} else {
try { Install-PackageProvider -Name "NuGet" -Force -ForceBootstrap -ErrorAction SilentlyContinue | Out-Null } catch {}
}
} else {
# NuGet should be included by default in PowerShell 7, so if it's not detected, advise reinstallation
Write-Warning "NuGet is not detected in PowerShell 7. Consider reinstalling PowerShell 7, as NuGet should be included by default."
}
} else {
# NuGet PackageProvider is already installed
Write-Debug "NuGet PackageProvider is already installed. Skipping installation."
}
}
# ============================================================================ #
# Initial checks
# ============================================================================ #
# Use global variables if specified by user
Import-GlobalVariable -VariableName "Debug"
Import-GlobalVariable -VariableName "ForceClose"
Import-GlobalVariable -VariableName "Force"
# First heading
Write-Output "winget-install $CurrentVersion"
# Check for updates if -CheckForUpdate is specified
if ($CheckForUpdate) { CheckForUpdate -RepoOwner $RepoOwner -RepoName $RepoName -CurrentVersion $CurrentVersion -PowerShellGalleryName $PowerShellGalleryName }
# Update the script if -UpdateSelf is specified
if ($UpdateSelf) { UpdateSelf }
# Heading
Write-Output "To check for updates, run winget-install -CheckForUpdate"
Write-Output "To delay script exit, run winget-install -Wait"
Write-Output "To force script pausing after execution, run winget-install -NoExit"
# Check if the current user is an administrator
if (-not (Test-AdminPrivileges)) {
Write-Warning "winget requires Administrator privileges to install. Please run the script as an Administrator and try again."
ExitWithDelay 1
}
# Get OS version
$osVersion = Get-OSInfo
# Get architecture type
$arch = $osVersion.Architecture
# Get current process module name to determine if launched in conhost
$currentProcess = Get-CurrentProcess
# If it's a workstation, make sure it is Windows 10+
if ($osVersion.Type -eq "Workstation" -and $osVersion.NumericVersion -lt 10) {
Write-Error "winget requires Windows 10 or later on workstations. Your version of Windows is not supported."
ExitWithDelay 1
}
# If it's a workstation with Windows 10, make sure it's version 1809 or greater
if ($osVersion.Type -eq "Workstation" -and $osVersion.NumericVersion -eq 10 -and $osVersion.ReleaseId -lt 1809) {