Installing PowerShell 7 Using Chocolatey
Chocolatey is a third-party package management tool for Windows. Chocolatey has a large set of packages you can install, and the Chocolatey tool (choco.exe
) provides a rich set of management features. You can install Chocolatey on both Windows Client machines (Windows 10/11 and earlier versions) and, as this recipe demonstrates, you can also install Chocolatey on Windows Server.
Chocolatey has a very large online registry of Windows packages that you can install, simplifying the deployment of applications in your environment. Read more about the company and its products at its website, https://chocolatey.org/.
Getting ready
You run this recipe on SRV1
after you have installed PowerShell 7. The method shown here installs PowerShell 7 using an MSI package. In the Installing PowerShell 7 recipe, you installed PowerShell 7 using the MSI. With PowerShell already installed, this recipe installs Chocolatey but would fail gracefully attempting to reinstall PowerShell 7. If you want to test the installation of PowerShell 7 using Chocolatey, you should remove PowerShell 7.
Run this script using the PowerShell ISE.
How to do it...
- Downloading the installation script for Chocolatey
$ChocoIns = 'C:\Foo\Install-Chocolatey.ps1' $DI = New-Object System.Net.WebClient $DI.DownloadString('https://community.chocolatey.org/install.ps1') | Out-File -FilePath $ChocoIns
- Viewing the installation help file
C:\Foo\Install-Chocolatey.ps1 -?
- Installing Chocolatey
C:\Foo\Install-Chocolatey.ps1
- Configuring Chocolatey
choco feature enable -n allowGlobalConfirmation
- Finding PowerShell (PWSH) on Chocolatey
choco find pwsh
- Installing PowerShell 7 using
choco.exe
choco install powershell-core –force
How it works...
In step 1, you download the Chocolatey installation script. You need this script to install Chocolatey. This step produces no output.
In step 2, you use Get-Help
to view the help information for the Chocolatey install script, with output like this:
Figure 1.7: Viewing the Chocolatey installation script help details
In step 3, you use the installation script to download and install Chocolatey on SRV1
. The output looks like this:
Figure 1.8: Installing Chocolatey
In step 4, you use choco.exe to set certain feature options with the following output:
Figure 1.9: Setting Chocolatey global options
In step 5, you use choco.exe to find PowerShell packages that you can install using Chocolatey.
The output looks like this:
Figure 1.10: Finding PowerShell on Chocolatey
In step 6, you install PowerShell 7 using Chocolatey. There is a lot of output, which looks like this:
Figure 1.11: Installing PowerShell 7
There’s more...
In step 1, you open a new Windows PowerShell 7 console. Make sure you run the console as the local administrator.
In step 6, you install PowerShell 7 (7.2.2, as you can see in the output). The result shows the successful installation of PowerShell.
If you do not uninstall PowerShell 7, then when you run this step, you will see different output, indicating that you have already installed the product, and thus, the installation fails gracefully.