One of the things that I always wondered about, is how you find out if strict mode is set in PowerShell session. I have put the problem aside until answer was posted to the Powershell.com forum. Finding out where and how the value is stored was the easy part. I just opened the .NET Reflector and looked at the definition of the SetStrictModeCommand. Getting the value from PowerShell session was much harder. Few days of casual digging and it turns out the answer can be retrieved using Reflection. As I am new to the whole Reflection thing it was pretty hard to get the basic principles but fortunately this POSHCode article came to rescue. It contains basically all I needed to extract the info.

function Get-Field
{
	[CmdletBinding()]
	param (
	[Parameter(Position=0,Mandatory=$true)]
	$InputObject
	)

	$type = $InputObject.gettype()

	$publicNonPublic = [Reflection.BindingFlags]::Public -bor [Reflection.BindingFlags]::NonPublic
	$instance = $publicNonPublic -bor [Reflection.BindingFlags]::Instance
	$getField = $instance -bor [Reflection.BindingFlags]::GetField

	$fields = $type.GetFields($instance)

	$result = @{}
	$fields | Foreach-Object { $result[$_.Name] = $type.InvokeMember($_.Name, $getField, $null, $InputObject, $null) }
	$result
}

function Get-StrictMode
{
	###if strict mode is set return its version, otherwise return nothing

	$context = (Get-Field $ExecutionContext)._context
	$sessionState = (Get-Field $context)._engineSessionState
	$scopes = Get-Field $sessionState
	$globalScope = Get-Field ($scopes._globalScope)

	#different hosts refer to the field differently trying to fix it
	try
	{
		$version = $globalScope."k__BackingField"
	}
	catch [System.Management.Automation.RuntimeException]
	{
		$errorId = $_.Exception.ErrorRecord.FullyQualifiedErrorId
		if (-not ($errorId -eq "PropertyNotFoundStrict"))
		{
			throw $_
		}
	}
	try
	{
		$version = $globalScope.StrictModeVersion
	}
	catch [System.Management.Automation.RuntimeException]
	{
		$errorId = $_.Exception.ErrorRecord.FullyQualifiedErrorId
		if (-not ($errorId -eq "PropertyNotFoundStrict"))
		{
			throw $_
		}
	}
	##Major version is grater than 0 if the strict mode is set
	#test if the variable is set first to avoid strict mode error
	if ($version)
	{
		if ($Version.Major -gt 0)
		{
			$version
		}
	}
}

Set-StrictMode -Version latest
Get-strictMode

Set-StrictMode -Version 2
Get-strictMode

Set-StrictMode -Version 1
Get-strictMode

Set-StrictMode -Off
Get-strictMode

The code is not perfect because only the state from global scope is retrieved. Ideally it should be retrieved from the current scope, but unfortunately I am unable to do so (Suggestions please.). In the current state the strict mode appears as not set unless you set it in to the global scope. This returns incorrect results:

&{
	Set-StrictMode -Version 2
	Get-StrictMode
}