In the previous tip we featured a backward-compatible one-liner that can tell whether your script is running in the classic Windows PowerShell environment or on the new PowerShell 7 portable shell.

If you do use the new platform-neutral PowerShell 7, then there is a new type called [Management.Automation.Platform] that returns even more platform information. This type did not yet exist in Windows PowerShell so you can use this type as well to determine whether you are currently running on Windows PowerShell. If not, the type provides additional platform guidance:

# testing whether type exists
$type = 'Management.Automation.Platform' -as [Type]
$isWPS = $type -eq $null

if ($isWPS)
{
  Write-Warning 'Windows PowerShell'
}
else
{
  # query all public properties
  $properties = $type.GetProperties().Name
  $properties | ForEach-Object -Begin { $hash = @{} } -Process { 
$hash[$_] = $type::$_ 
} -End { $hash }
}

On Windows PowerShell, the script just produces a warning. On PowerShell 7, it returns a hash table instead with all relevant platform information:

 
Name                           Value
----                           -----
IsStaSupported                 True
IsLinux                        False
IsCoreCLR                      True
IsWindows                      True
IsNanoServer                   False
IsMacOS                        False
IsWindowsDesktop               True
IsIoT                          False
 




Twitter This Tip! ReTweet this Tip!

Anonymous