Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions .vsts-ci/install-ps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,84 @@ phases:
{
throw "powershell was not upgraded: $newVersion"
}

- template: templates/install-ps-phase.yml
parameters:
scriptName: |
pwsh -c ./tools/install-powershell.ps1 -AddToPath -Version 7.3.6
jobName: InstallPowerShellWindowsVersion
pool: windows-latest
verification: |
$newVersion = &$env:LOCALAPPDATA\Microsoft\powershell-7.3.6\pwsh -v
$newVersion -match '^PowerShell ((\d*\.\d*\.\d*)(-\w*(\.\d*)?)?){1}'
$versionOnly = $Matches[2]
Write-verbose "$newVersion; versionOnly: $versionOnly" -verbose
if ([Version]$versionOnly -lt [version]"7.0.0")
{
throw "powershell was not upgraded: $newVersion"
}

- template: templates/install-ps-phase.yml
parameters:
scriptName: |
pwsh -c ./tools/install-powershell.ps1 -AddToPath -Version 7.3.6 -Destination "${LOCALAPPDATA}/Microsoft/pwsh"
jobName: InstallPowerShellWindowsVersionDestination
pool: windows-latest
verification: |
$newVersion = &$env:LOCALAPPDATA\Microsoft\pwsh-7.3.6\pwsh -v
$newVersion -match '^PowerShell ((\d*\.\d*\.\d*)(-\w*(\.\d*)?)?){1}'
$versionOnly = $Matches[2]
Write-verbose "$newVersion; versionOnly: $versionOnly" -verbose
if ([Version]$versionOnly -ne [version]"7.3.6")
{
throw "powershell $newVersion wasn't installed to custom destination"
}

- template: templates/install-ps-phase.yml
parameters:
scriptName: |
pwsh -c ./tools/install-powershell.ps1 -AddToPath -LTS
jobName: InstallPowerShellWindowsLTS
pool: windows-latest
verification: |
$newVersion = &$env:LOCALAPPDATA\Microsoft\powershell-lts\pwsh -v
$newVersion -match '^PowerShell ((\d*\.\d*\.\d*)(-\w*(\.\d*)?)?){1}'
$versionOnly = $Matches[2]
Write-verbose "$newVersion; versionOnly: $versionOnly" -verbose
if ([Version]$versionOnly -lt [version]"7.0.0")
{
throw "powershell was not upgraded: $newVersion"
}


- template: templates/install-ps-phase.yml
parameters:
scriptName: |
pwsh -c ./tools/install-powershell.ps1 -AddToPath -Preview
jobName: InstallPowerShellWindowsPreview
pool: windows-latest
verification: |
$newVersion = &$env:LOCALAPPDATA\Microsoft\powershell-preview\pwsh -v
$newVersion -match '^PowerShell ((\d*\.\d*\.\d*)(-\w*(\.\d*)?)?){1}'
$versionOnly = $Matches[2]
Write-verbose "$newVersion; versionOnly: $versionOnly" -verbose
if ([Version]$versionOnly -lt [version]"7.0.0")
{
throw "powershell was not upgraded: $newVersion"
}

- template: templates/install-ps-phase.yml
parameters:
scriptName: |
pwsh -c ./tools/install-powershell.ps1 -AddToPath -Stable
jobName: InstallPowerShellWindowsStable
pool: windows-latest
verification: |
$newVersion = &$env:LOCALAPPDATA\Microsoft\powershell-stable\pwsh -v
$newVersion -match '^PowerShell ((\d*\.\d*\.\d*)(-\w*(\.\d*)?)?){1}'
$versionOnly = $Matches[2]
Write-verbose "$newVersion; versionOnly: $versionOnly" -verbose
if ([Version]$versionOnly -lt [version]"7.0.0")
{
throw "powershell was not upgraded: $newVersion"
}
112 changes: 80 additions & 32 deletions tools/install-powershell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,45 @@
Invoke this script directly from GitHub
Invoke-Expression "& { $(Invoke-RestMethod 'https://aka.ms/install-powershell.ps1') } -daily"
#>
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '')]
[CmdletBinding(DefaultParameterSetName = "Daily")]
param(
[Parameter(ParameterSetName = "Daily")]
[string] $Destination,
[Parameter(ParameterSetName = "Version" )]
[Parameter(ParameterSetName = "MSI")]
[string] $Version,
Comment thread
kilasuit marked this conversation as resolved.
Resolved

[Parameter(ParameterSetName = "Stable")]
[Parameter(ParameterSetName = "MSI")]
[switch] $Stable,

[Parameter(ParameterSetName = "LTS")]
[Parameter(ParameterSetName = "MSI")]
[switch] $LTS,

[Parameter(ParameterSetName = "Daily")]
[switch] $Daily,

[Parameter(ParameterSetName = 'Preview')]
[Parameter(ParameterSetName = "MSI")]
[switch] $Preview,

[Parameter(ParameterSetName = "Daily" )]
[Parameter(ParameterSetName = "Version")]
[Parameter(ParameterSetName = "LTS")]
[Parameter(ParameterSetName = "Preview")]
[Parameter(ParameterSetName = "Stable")]
[string] $Destination,

[Parameter(ParameterSetName = "Daily")]
[Parameter(ParameterSetName = "Version")]
[Parameter(ParameterSetName = "LTS")]
Comment thread
kilasuit marked this conversation as resolved.
Resolved
[Parameter(ParameterSetName = 'Preview')]
[Parameter(ParameterSetName = "Stable")]
[switch] $DoNotOverwrite,

[Parameter(ParameterSetName = "Daily")]
[switch] $AddToPath,

[Parameter(ParameterSetName = "MSI")]
[Parameter(ParameterSetName = "MSI", Mandatory)]
[switch] $UseMSI,

[Parameter(ParameterSetName = "MSI")]
Expand All @@ -48,10 +72,7 @@ param(
[switch] $AddExplorerContextMenu,

[Parameter(ParameterSetName = "MSI")]
[switch] $EnablePSRemoting,

[Parameter()]
[switch] $Preview
[switch] $EnablePSRemoting
)

Set-StrictMode -Version 3.0
Expand All @@ -61,16 +82,42 @@ $IsLinuxEnv = (Get-Variable -Name "IsLinux" -ErrorAction Ignore) -and $IsLinux
$IsMacOSEnv = (Get-Variable -Name "IsMacOS" -ErrorAction Ignore) -and $IsMacOS
$IsWinEnv = !$IsLinuxEnv -and !$IsMacOSEnv

if ($version) {
# if version doesnt contains v prefix, add it
if ($version -notmatch "^v") {
$version = "v$version"
}

Write-Verbose "Checking if release version '$version' is available" -Verbose
$releaseVersions = Invoke-RestMethod https://api.github.com/repos/PowerShell/PowerShell/releases -Verbose:$false
Comment thread
TravisEz13 marked this conversation as resolved.
Resolved

if ("$version" -notin $releaseVersions.tag_name) {
throw "Version '$version' is not available. Available versions: $($releaseVersions.tag_name -join ', ')"
} else {
Write-Verbose "Version '$version' is available. Installing as requested"
}

# if version contains v prefix, remove it
$version = $version -replace "^v", ""
}

if (-not $Destination) {
if ($IsWinEnv) {
$Destination = "$env:LOCALAPPDATA\Microsoft\powershell"
} else {
$Destination = "~/.powershell"
}

if ($Daily) {
$Destination = "${Destination}-daily"
}
}
if ($version) {
$Destination = "${Destination}-${version}"
} elseif ($Daily) {
$Destination = "${Destination}-daily"
} elseif ($LTS) {
$Destination = "${Destination}-lts"
} elseif ($Preview) {
$Destination = "${Destination}-preview"
} elseif ($Stable) {
$Destination = "${Destination}-stable"
}

$Destination = $PSCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Destination)
Expand All @@ -82,10 +129,10 @@ if (-not $UseMSI) {
throw "-UseMSI is only supported on Windows"
} else {
$MSIArguments = @()
if($AddExplorerContextMenu) {
if ($AddExplorerContextMenu) {
$MSIArguments += "ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1"
}
if($EnablePSRemoting) {
if ($EnablePSRemoting) {
$MSIArguments += "ENABLE_PSREMOTING=1"
}
}
Expand All @@ -100,16 +147,13 @@ function Expand-ArchiveInternal {
$DestinationPath
)

if((Get-Command -Name Expand-Archive -ErrorAction Ignore))
{
if ((Get-Command -Name Expand-Archive -ErrorAction Ignore)) {
Expand-Archive -Path $Path -DestinationPath $DestinationPath
}
else
{
} else {
Add-Type -AssemblyName System.IO.Compression.FileSystem
$resolvedPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
$resolvedDestinationPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($DestinationPath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($resolvedPath,$resolvedDestinationPath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($resolvedPath, $resolvedDestinationPath)
}
}

Expand Down Expand Up @@ -201,7 +245,7 @@ Function Add-PathTToSettings {
param(
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[ValidateScript({Test-PathNotInSettings $_})]
[ValidateScript({ Test-PathNotInSettings $_ })]
[string] $Path,

[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
Expand Down Expand Up @@ -270,12 +314,12 @@ try {
$release = $metadata.ReleaseTag -replace '^v'

# Get version from currently installed PowerShell Daily if available.
$pwshPath = if ($IsWinEnv) {Join-Path $Destination "pwsh.exe"} else {Join-Path $Destination "pwsh"}
$currentlyInstalledVersion = if(Test-Path $pwshPath) {
$pwshPath = if ($IsWinEnv) { Join-Path $Destination "pwsh.exe" } else { Join-Path $Destination "pwsh" }
Comment thread
daxian-dbw marked this conversation as resolved.
Resolved
$currentlyInstalledVersion = if (Test-Path $pwshPath) {
((& $pwshPath -version) -split " ")[1]
}

if($currentlyInstalledVersion -eq $release) {
if ($currentlyInstalledVersion -eq $release) {
Write-Verbose "Latest PowerShell Daily already installed." -Verbose
return
}
Expand Down Expand Up @@ -320,16 +364,16 @@ try {
if ($IsWinEnv) {
if ($UseMSI -and $Quiet) {
Write-Verbose "Performing quiet install"
$ArgumentList=@("/i", $packagePath, "/quiet")
if($MSIArguments) {
$ArgumentList+=$MSIArguments
$ArgumentList = @("/i", $packagePath, "/quiet")
if ($MSIArguments) {
$ArgumentList += $MSIArguments
}
$process = Start-Process msiexec -ArgumentList $ArgumentList -Wait -PassThru
if ($process.exitcode -ne 0) {
throw "Quiet install failed, please rerun install without -Quiet switch or ensure you have administrator rights"
}
} elseif ($UseMSI) {
if($MSIArguments) {
if ($MSIArguments) {
Start-Process $packagePath -ArgumentList $MSIArguments -Wait
} else {
Start-Process $packagePath -Wait
Expand All @@ -344,6 +388,10 @@ try {
$metadata = Invoke-RestMethod https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/metadata.json
if ($Preview) {
$release = $metadata.PreviewReleaseTag -replace '^v'
} elseif ($version) {
$release = $version
} elseif ($LTS) {
$release = $metadata.LTSReleaseTag -replace '^v'
} else {
$release = $metadata.ReleaseTag -replace '^v'
}
Expand Down Expand Up @@ -384,16 +432,16 @@ try {
if ($IsWinEnv) {
if ($UseMSI -and $Quiet) {
Write-Verbose "Performing quiet install"
$ArgumentList=@("/i", $packagePath, "/quiet")
if($MSIArguments) {
$ArgumentList+=$MSIArguments
$ArgumentList = @("/i", $packagePath, "/quiet")
if ($MSIArguments) {
$ArgumentList += $MSIArguments
}
$process = Start-Process msiexec -ArgumentList $ArgumentList -Wait -PassThru
if ($process.exitcode -ne 0) {
throw "Quiet install failed, please rerun install without -Quiet switch or ensure you have administrator rights"
}
} elseif ($UseMSI) {
if($MSIArguments) {
if ($MSIArguments) {
Start-Process $packagePath -ArgumentList $MSIArguments -Wait
} else {
Start-Process $packagePath -Wait
Expand Down
17 changes: 15 additions & 2 deletions tools/install-powershell.ps1-README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,30 @@
* Automatically looks up latest version via git tags
* Optionally installs silently
* Optionally adds the install location to Path environment variable
* Optionally allows for installation of specified version
* Optionally installs the LTS version

## Examples

### Install PowerShell Core Daily Build
### Install PowerShell Daily Build

```PowerShell
Invoke-Expression "& { $(Invoke-RestMethod 'https://aka.ms/install-powershell.ps1') } -daily"
```

### Install PowerShell Core using the MSI installer
### Install PowerShell using the MSI installer

```PowerShell
Invoke-Expression "& { $(Invoke-RestMethod 'https://aka.ms/install-powershell.ps1') } -UseMSI"
```

### Install PowerShell version 7.3.1

```PowerShell
Invoke-Expression "& { $(Invoke-RestMethod 'https://aka.ms/install-powershell.ps1') } -Destination 'C:\ps' -Version '7.3.1'"
```
### Install Current LTS version of PowerShell (7.2.12)

```PowerShell
Invoke-Expression "& { $(Invoke-RestMethod 'https://aka.ms/install-powershell.ps1') } -Destination 'C:\ps' -LTS"
```