0

Windows: Assuming that PowerShell Core is not installed on Windows scenario.

Linux : Assuming that PowerShell Core was not installed by default and need to make a shell script to install it scenario.

MacOS : Assuming that Tried other method to download latest PS Core than Microsoft provided script, but failed. Removing the files, but found leftovers that must be removed by the script


How to download and install latest PowerShell Core of a specified channel (release , beta, rc) for specified OS (-win- ; -osx ; -1.ubuntu-18.04-) with specified architecture (-amd64 ; -x86 ; -x64) using bash or powershell? (Without requiring manual config)

Here's the code :

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$location = Get-Location
$architecture = "x" + (Get-WmiObject Win32_Processor).AddressWidth
$assetName = "PowerShell-*-win-$architecture.msi"

$gitHubApi = 'https://api.github.com/repos/PowerShell/PowerShell/releases/latest'
$response = Invoke-WebRequest -Uri $gitHubApi -UseBasicParsing
$json = $response.Content | ConvertFrom-Json
$release = $json.assets | Where-Object Name -like $assetName


Invoke-WebRequest $release.browser_download_url -OutFile "$location\$($release.name)"

Start-Process msiexec.exe -Wait -ArgumentList '/A $($release.name) /I $location\$($release.name) /quiet /qb ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1 REGISTER_MANIFEST=1 ENABLE_PSREMOTING=1'

Write-Host Ok
WinScriptDev
  • 15
  • 1
  • 9
  • 1
    Please edit your original question to include details on what code you have tried, what error messages you got or how the result was different from what you expected. – notjustme Mar 13 '20 at 13:58
  • 1
    There is `https://aka.ms/install-powershell.ps1`, which, of course, requires that _some_ version of PowerShell is already installed, so it works on _Windows_, where you can run from Windows PowerShell, but not on Unix-like platforms. On Unix-like platforms, the simplest way to install is via the various package managers or direct downloads - see [instructions for Linux distros](https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux) and for [macOS](https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-macos). – mklement0 Mar 13 '20 at 14:38
  • I assume that the code you're showing already works _on Windows_, and now you're looking for something analogous on _Unix-like_ platforms? If so, please make that clearer in your question. – mklement0 Mar 13 '20 at 15:11
  • Question: When i tested ```install-powershell.ps1``` , it actually download and installs latest ```PowerShell Core``` for Windows, i noticed it doesn't appear as a program i.e. shows at star menu. Why? And, do y'know a way to download powershell core without powershell? – WinScriptDev Mar 13 '20 at 17:22
  • Your handle is WinScriptDev, which makes one assume you've been at this scripting thing for a while. So, for this download without PowerShell question. Why are you not looking to use VBScript or WMI for that? Both are capable tools for such use cases. Example: https://stackoverflow.com/questions/2973136/download-a-file-with-vbs. Thus using terminals on non-Win. Example: https://osxdaily.com/2007/05/11/download-files-from-the-web-via-the-os-x-command-line/ – postanote Mar 13 '20 at 21:43
  • 1
    There is also `https://aka.ms/install-powershell.sh` – Giorgi Chakhidze Mar 14 '20 at 00:11

0 Answers0