Private/Test/Test-IsPowerShellCore.ps1
|
function Test-IsPowerShellCore { <# .SYNOPSIS Tests if the current session is running PowerShell Core/7+ (Core edition). .DESCRIPTION Checks if the PowerShell session is running PowerShell Core/7+ (version 6.0+) by verifying the PSEdition is 'Core'. This distinguishes between: - Windows PowerShell (Desktop edition) - versions 5.1 and earlier, Windows-only - PowerShell Core/7+ (Core edition) - versions 6.0+, cross-platform Use this function when you need to conditionally execute code that requires PowerShell Core/7+ features or check for cross-platform compatibility. .INPUTS None. This function does not accept pipeline input. .OUTPUTS System.Boolean Returns $true if running PowerShell Core/7+ (Core edition). Returns $false if running Windows PowerShell (Desktop edition). .EXAMPLE Test-IsPowerShellCore Returns $true if running PowerShell Core/7+. .EXAMPLE if (Test-IsPowerShellCore) { Write-Host "Running PowerShell Core/7+ - cross-platform features available" } else { Write-Host "Running Windows PowerShell - Desktop edition" } Conditionally executes code based on the PowerShell edition. .EXAMPLE if (Test-IsPowerShellCore) { # Use PowerShell Core/7+ specific features Import-Module PwshSpectreConsole } Loads Core-compatible modules only in PowerShell Core/7+. .NOTES PowerShell Editions: - Desktop: Windows PowerShell 5.1 and earlier (Windows-only, .NET Framework) - Core: PowerShell 6.0+ (cross-platform, .NET Core/.NET 5+) The PSEdition property was introduced in PowerShell 5.1. .LINK https://learn.microsoft.com/en-us/powershell/scripting/install/powershell-support-lifecycle .LINK https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables#psedition #> [CmdletBinding()] [OutputType([bool])] param ( ) #requires -Version 5.1 # Check if running PowerShell Core/7+ (Core edition) vs Windows PowerShell (Desktop edition) [bool]($PSEdition -eq 'Core') } |