-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Add 'install-powershell.ps1' to install powershell core on windows #5383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| <# | ||
| .Synopsis | ||
| Install PowerShell Core on Windows. | ||
| .DESCRIPTION | ||
| By default, the latest PowerShell Core release package will be installed. | ||
| If '-Daily' is specified, then the latest PowerShell Core daily package will be installed. | ||
| .Parameter Destination | ||
| The destination path to install PowerShell Core to. | ||
| .Parameter Daily | ||
| Install PowerShell Core from the daily build. | ||
| Note that the 'PackageManagement' module is required to install a daily package. | ||
| .Parameter DoNotOverwrite | ||
| Do not overwrite the destination folder if it already exists. | ||
| .Parameter AddToPath | ||
| Add the absolute destination path to the 'User' scope environment variable 'Path' | ||
| #> | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter()] | ||
| [ValidateNotNullOrEmpty()] | ||
| [string] $Destination, | ||
|
|
||
| [Parameter()] | ||
| [switch] $Daily, | ||
|
|
||
| [Parameter()] | ||
| [switch] $DoNotOverwrite, | ||
|
|
||
| [Parameter()] | ||
| [switch] $AddToPath | ||
| ) | ||
|
|
||
| Set-StrictMode -Version latest | ||
| $ErrorActionPreference = "Stop" | ||
|
|
||
| if (-not $Destination) { | ||
| $Destination = "$env:LOCALAPPDATA\Microsoft\powershell" | ||
| if ($Daily) { | ||
| $Destination = "${Destination}-daily" | ||
| } | ||
| } | ||
|
|
||
| if (Test-Path -Path $Destination) { | ||
| if ($DoNotOverwrite) { | ||
| throw "Destination folder '$Destination' already exist. Use a different path or omit '-DoNotOverwrite' to overwrite." | ||
| } | ||
| Remove-Item -Path $Destination -Recurse -Force | ||
| } | ||
| New-Item -ItemType Directory -Path $Destination -Force > $null | ||
| $Destination = Resolve-Path -Path $Destination | ForEach-Object -MemberName Path | ||
| Write-Verbose "Destination: $Destination" -Verbose | ||
|
|
||
| $architecture = switch ($env:PROCESSOR_ARCHITECTURE) { | ||
| "AMD64" { "x64" } | ||
| "x86" { "x86" } | ||
| default { throw "PowerShell package for OS architecture '$_' is not supported." } | ||
| } | ||
| $tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName()) | ||
| New-Item -ItemType Directory -Path $tempDir -Force > $null | ||
| try { | ||
| if ($Daily) { | ||
| if (-not (Get-Module -Name PackageManagement -ListAvailable)) { | ||
| throw "PackageManagement module is required to install daily PowerShell Core." | ||
| } | ||
|
|
||
| if ($architecture -ne "x64") { | ||
| throw "The OS architecture is '$architecture'. However, we currently only support daily package for x64 Windows." | ||
| } | ||
|
|
||
| ## Register source if not yet | ||
| if (-not (Get-PackageSource -Name powershell-core-daily -ErrorAction SilentlyContinue)) { | ||
| $packageSource = "https://powershell.myget.org/F/powershell-core-daily" | ||
| Write-Verbose "Register powershell-core-daily package source '$packageSource' with PackageManagement" -Verbose | ||
| Register-PackageSource -Name powershell-core-daily -Location $packageSource -ProviderName nuget -Trusted > $null | ||
| } | ||
|
|
||
| $packageName = "powershell-win-x64-win7-x64" | ||
| $package = Find-Package -Source powershell-core-daily -AllowPrereleaseVersions -Name $packageName | ||
| Write-Verbose "Daily package found. Name: $packageName; Version: $($package.Version)" -Verbose | ||
| Install-Package -InputObject $package -Destination $tempDir -ExcludeVersion > $null | ||
|
|
||
| $contentPath = [System.IO.Path]::Combine($tempDir, $packageName, "content") | ||
| Copy-Item -Path $contentPath\* -Destination $Destination -Recurse -Force | ||
| } else { | ||
| $metadata = Invoke-RestMethod https://api.github.com/repos/powershell/powershell/releases/latest | ||
| $release = $metadata.tag_name -replace '^v' | ||
|
|
||
| $packageName = "PowerShell-${release}-win-${architecture}.zip" | ||
| $downloadURL = "https://github.com/PowerShell/PowerShell/releases/download/v${release}/${packageName}" | ||
| Write-Verbose "About to download package from '$downloadURL'" -Verbose | ||
|
|
||
| $packagePath = Join-Path -Path $tempDir -ChildPath $packageName | ||
| Invoke-WebRequest -Uri $downloadURL -OutFile $packagePath | ||
|
|
||
| Expand-Archive -Path $packagePath -DestinationPath $Destination | ||
| } | ||
|
|
||
| if ($AddToPath -and (-not $env:Path.Contains($Destination))) { | ||
| ## Add to the User scope 'Path' environment variable | ||
| $userPath = [System.Environment]::GetEnvironmentVariable("Path", "User") | ||
| $userPath = $Destination + [System.IO.Path]::PathSeparator + $userPath | ||
| [System.Environment]::SetEnvironmentVariable("Path", $userPath, "User") | ||
|
|
||
| ## Add to the 'Path' for the current process | ||
| $env:Path = $Destination + [System.IO.Path]::PathSeparator + $env:Path | ||
| Write-Verbose "'$Destination' is added to Path" -Verbose | ||
| } | ||
|
|
||
| Write-Host "PowerShell Core has been installed at $Destination" -ForegroundColor Green | ||
| } finally { | ||
| Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a case where the user wouldn't addtopath? Seems like this would be the primary use case? Perhaps have
-DoNotAddToPathinstead?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think when installing daily build package, a user possibly doesn't want it to be added to Path by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking the opposite where for daily self-hosting, I would want it in the path, although it would only need to be added once, so this isn't a big deal. I'm fine leaving it the way it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I will leave it unchanged.