Private/Test/Test-IsWindows.ps1
|
function Test-IsWindows { <# .SYNOPSIS Tests if the current session is running on Windows. .DESCRIPTION Returns $true if running on a Windows operating system. Works across Windows PowerShell 5.1 and PowerShell 6+ (cross-platform). In PowerShell 5.1 or earlier, always returns $true (Windows-only versions). In PowerShell 6+, checks the $IsWindows automatic variable. .INPUTS None. This function does not accept pipeline input. .OUTPUTS System.Boolean Returns $true if running on Windows, $false otherwise. .EXAMPLE Test-IsWindows Returns $true if the current session is running on Windows. .EXAMPLE if (Test-IsWindows) { Write-Host "Running on Windows" } Conditionally executes code only on Windows systems. .LINK https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_automatic_variables#iswindows #> [CmdletBinding()] [OutputType([bool])] param ( ) #requires -Version 5.1 $result = [bool](($PSVersionTable.PSVersion.Major -le 5) -or $IsWindows) Write-Verbose "Windows OS: $result" $result } |