# Module-PowerShell7-Require.ps1

# Table of Contents

  • Purpose
  • Dependencies
  • Function
  • Usage
  • Inputs
  • Variables
  • Command Line Flags
  • Outputs
  • Notes

# Purpose

Ensures the script is running in PowerShell 7 (pwsh). If not, attempts to relaunch itself in PowerShell 7, or offers to install it or open the official install page. This is a robust enforcement and user guidance utility for cross-platform and modern PowerShell script compatibility.

# Dependencies

# Function

  1. Checks if the script is running in PowerShell 7 (Core, version 7+).
  2. If already running in PowerShell 7, continues execution.
  3. If not, attempts to relaunch itself in PowerShell 7 using pwsh.
  4. If pwsh is not found, prompts the user to:
    • Install PowerShell 7.4.1 automatically (silent install, requires admin)
    • Open the official install website
    • Do nothing and exit
  5. Handles download and silent install of PowerShell 7.4.1 if chosen and running as admin.
  6. Provides clear, accessible output and error handling for all scenarios.

# Usage

# To use this function from another script, you must dot-source the file first:
. "C:\Path\To\Module-PowerShell7-Require.ps1"
Require-Pwsh7
# Or, to specify a script to relaunch:
Require-Pwsh7 -ScriptToRelaunch "C:\Path\To\Script.ps1"

Note: Dot-sourcing loads the function into your session so it can be called. Adjust the path as needed for your environment.

# Inputs

  • -ScriptToRelaunch (string, optional): Path to the script to relaunch in PowerShell 7. Defaults to the current script.

# Variables

  • $ScriptToRelaunch: Script path to relaunch if not in PowerShell 7.
  • $PSVersionTable: Used to check current PowerShell version and edition.
  • $installerPath: Temporary path for the PowerShell 7 MSI installer.
  • $downloadUrl: URL for the PowerShell 7.4.1 MSI download.
  • $choice: User's menu selection for install/open/exit.

# Command Line Flags

  • -ScriptToRelaunch: Specifies the script to relaunch in PowerShell 7 (optional).

# Outputs

  • Relaunches the script in PowerShell 7 if needed.
  • Installs PowerShell 7.4.1 if chosen and running as admin.
  • Opens the official install page if chosen.
  • Exits with a message if PowerShell 7 is not available and not installed.
  • Provides clear user feedback and error messages for all actions.

# Notes

  • This function is intended to be dot-sourced or included at the top of scripts that require PowerShell 7.
  • Handles both interactive and automated scenarios robustly.
  • Requires internet access for automatic install.
  • All output is accessible and uses EN-AU spelling.
  • See project standards for robust root-based module import and error handling.