3

it seems Powershell v5.1 uses .Net Framework v4.0

[Environment]::Version # Major Minor Build Revision # ----- ----- ----- -------- # 4 0 30319 42000 

and DateTimeOffset.FromUnixTimeSeconds method requires .Net Framework v4.6+ , so basically Powershell v5.1 should not have access to it. but ...

[DateTimeOffset]|Get-Member -Static -Name FromUnixTimeSeconds # TypeName: System.DateTimeOffset # Name MemberType Definition # ---- ---------- ---------- # FromUnixTimeSeconds Method static System.DateTimeOffset FromUnixTimeSeconds(long seconds) 

Why am I able to call it? Does it have something to do with C# AssemblyVersion & AssemblyFileVersion?

1 Answer 1

9

[Environment]::Version reports the CLR (the runtime) version, which is not the same as the .NET Framework version, which is the independently versioned class library[1] that builds on a given CLR version.

To get the framework version, use the following, which returns a string such as
.NET Framework 4.8.4341.0:

[System.Runtime.InteropServices.RuntimeInformation]::FrameworkDescription 

Note:

  • This property is available in .NET Framework 4.7.1 and above, and Windows PowerShell in recent Windows versions is built on v4.8.

    • Caveat: As Bacon Bits reports, version 2.0 of the PSReadLineModule module can shadow the property due to inexplicably being bundled with an older version of the [System.Runtime.InteropServices.RuntimeInformation] class. Windows PowerShell versions up to at least Windows 11 22H2 ship with 2.0, so you must manually upgrade the PSReadLine module to version 2.1 or higher (2.2.6 is current as of this writing), e.g. with
      Install-Module -Force PSReadLine -Scope CurrentUser; see the comments for details.
  • If it isn't available, you can try the following, which yields a string such as "4.8.04084":[2]

    Get-ItemPropertyValue 'registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full' Version 
  • If that doesn't work either, the implication is that your .NET Framework version is lower than 4.5


[1] In the cross-platform .NET Core / .NET 5+ edition, there is only a unified runtime that also includes the class library, which [Environment]::Version correctly reflects in .NET Core 3.0 and later.

[2] Curiously, this version differs in the revision number from the one reported by [System.Runtime.InteropServices.RuntimeInformation]::FrameworkDescription (if available). There's yet another variant with a differing revision, as part of the InstallPath value from the same registry key; e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319\

Sign up to request clarification or add additional context in comments.

7 Comments

Interesting. For me, [System.Runtime.InteropServices.RuntimeInformation] in Windows Powershell v5.1 has no static property FrameworkDescription. I'm on Windows 10 20H2, and the only update I'm currently lacking is the 21H1 update.
Yes, the registry does work. I get 4.8.04084. New instances of WinPS don't change anything, and neither does running as admin. Maybe I've gotten a CLR update and I need to reboot my workstation.
A reboot doesn't fix anything, but tab completion on the RuntimeInformation class gave me [System.Runtime.InteropServices.RuntimeInformation, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]::FrameworkDescription, and that works. I get .NET Framework 4.8.4360.0.
I've updated PSReadLine for Windows Powershell to 2.1.0 and now it works as expected. That was silly.
@BaconBits: Great sleuthing; that this type would be included in a PSReadLine-specific assembly is both curious and a little frightening. Glad to hear that upgrading to the latest stable version solved the problem.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.