Installing PowerShell 7.x

Installing PowerShell 7.x

On a Windows system, PowerShell 2.0 is included and installed by default. This old version is not necessary and can be uninstalled via the Control Panel. The steps below explain how you can start installing PowerShell 7.x.

  1. Open PowerShell as Administrator
    • Use Win + X and select Windows Terminal (Admin).
  2. Install PowerShell using the following command:
    • winget install --id Microsoft.Powershell --source winget

This command downloads and installs the latest stable version of PowerShell. On new systems, WinGet may not have been installed. A good suggestion would be to first run the following command. It will tell you what to install first.

# Begin Function Confirm-Prerequisites
Function Confirm-Prerequisites {
    $modules = @(
        @{ Name = "Az" },
        @{ Name = "Microsoft.Graph" }
    )

    if (-not $global:installed) {
        foreach ($module in $modules) {
            $installedModule = Get-InstalledModule -Name $module.Name -ErrorAction SilentlyContinue
            if ($null -eq $installedModule) {
                Write-Host "$($module.Name) is not installed. Installing the latest version..."
                Install-Module -Name $module.Name -Force -Scope CurrentUser
            } else {
                $latestModule = Find-Module -Name $module.Name
                if ($installedModule.Version -lt $latestModule.Version) {
                    Write-Host "$($module.Name) is installed but not the latest version. Updating to version $($latestModule.Version)..."
                    Update-Module -Name $module.Name -Force
                } else {
                    Write-Host "$($module.Name) is installed and up-to-date. Version: $($installedModule.Version)"
                }
            }
        }

        # Check if Azure CLI is installed
        if (Get-Command az -ErrorAction SilentlyContinue) {
            Write-Host "Azure CLI is installed."
        } else {
            Write-Host "Azure CLI is not installed. Please install it from https://aka.ms/InstallAzureCli"
        }

        # Check if Winget is installed (only if not on Windows Server)
        $osCaption = (Get-WmiObject -Class Win32_OperatingSystem).Caption
        if ($osCaption -match "Windows Server") {
            Write-Host "Skipping Winget check on Windows Server."
        } else {
            if (Get-Command winget -ErrorAction SilentlyContinue) {
                $wingetVersion = winget --version 2>&1 | Out-String
                if ($wingetVersion -match "v(\d+\.\d+\.\d+)") {
                    Write-Host "Winget is installed. Version: $($matches[1])"
                } else {
                    Write-Host "Winget is installed but version could not be determined."
                }
            } else {
                Write-Host "Winget is not installed. Please install it from https://aka.ms/GetWinget"
            }
        }

        $global:installed = $true
        Write-Host "Installation was successful. All prerequisites met. No further action needed."
    } else {
        Write-Host "All prerequisites met. No further action needed."
    }
}
# End Function Confirm-Prerequisites

Confirm-Prerequisites

Method 2: Installing PowerShell 7.x via MSI Package

  1. Download the MSI Installer
    Visit the PowerShell GitHub releases page and download the appropriate MSI package for your Windows 11 system.
  2. Run the Installer
    Open the downloaded MSI file and follow the installation instructions.
  3. Verify the Installation
    • After installation, open PowerShell and run:
      • $PSVersionTable

This command will display the installed version of PowerShell. If you see this, PowerShell 7.x has been successfully installed.

Name                           Value
----                           -----
PSVersion                      7.5.0
PSEdition                      Core
GitCommitId                    7.5.0
OS                             Microsoft Windows 10.0.26100
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

If this is not the case, you can, for example, perform the following steps.

Set PowerShell 7.x as Default

Update Environment Variables

  1. Press Win + X and select System.
  2. Click on Advanced system settings.
  3. In the System Properties window, click Environment Variables.
  4. Under System Variables, locate the Path variable, select it, and click Edit.
  5. Add the path to the PowerShell 7.x executable (e.g., C:\Program Files\PowerShell\7) to the list.

Create a Shortcut

  1. Right-click on the desktop and select New > Shortcut.
  2. For the location, enter the path to pwsh.exe (e.g., C:\Program Files\PowerShell\7\pwsh.exe).
  3. Name the shortcut “PowerShell 7” and click Finish.

Update PowerShell Profile

  1. Open PowerShell 7.x and run: powershellCopyEditif (!(Test-Path -Path $PROFILE)) { New-Item -Type File -Path $PROFILE -Force } notepad $PROFILE
  2. Add the following line to the profile script: powershellCopyEdit$env:Path = "C:\Program Files\PowerShell\7;" + $env:Path

This ensures PowerShell 7.x is used in the environment path.

If You Prefer PowerShell ISE

Running the following in PowerShell ISE will show output like:

powershellCopyEditPS C:\WINDOWS\system32> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.26100.2161
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.26100.2161
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

This is because this version of PowerShell launches from:

shellCopyEdit%systemroot%\System32\WindowsPowerShell

For More Capabilities, Use Visual Studio Code (VS Code).

Benefits of VS Code with the PowerShell Extension

  1. Actively Maintained: VS Code and the PowerShell extension receive regular updates and new features.
  2. Cross-Platform: Works on Windows, macOS, and Linux.
  3. Advanced Features: Includes IntelliSense, debugging, integrated terminal, and Git support.
  4. Customizable: You can add extensions and themes to suit your workflow.
  5. Community Support: Large, active community with tutorials and shared extensions.

Switching to VS Code

  1. Install VS Code
  2. Install PowerShell Extension
    • Open VS Code
    • Click the Extensions icon in the Activity Bar.
    • Search for “PowerShell” and install the Microsoft extension.
  3. Configure PowerShell Version
    • Open the Command Palette (Ctrl+Shift+P)
    • Search for and select PowerShell: Show Session Menu
    • Choose the installed PowerShell version.

Conclusion

If you prefer the simplicity and familiarity of PowerShell ISE, you can continue using it for basic tasks. However, for more advanced development and future-proof skills, switching to Visual Studio Code is recommended.

For more information about PowerShell, visit the Microsoft site. More about the author can be found on the Digitale Mels site.

0 Shares:
You May Also Like