@@ -20,15 +20,20 @@ internal static class VersionUtils
20
20
public static bool IsNetCore { get ; } = RuntimeInformation . FrameworkDescription . StartsWith ( ".NET Core" , StringComparison . Ordinal ) ;
21
21
22
22
/// <summary>
23
- /// Get's the Version of PowerShell being used.
23
+ /// Gets the Version of PowerShell being used.
24
24
/// </summary>
25
25
public static Version PSVersion { get ; } = PowerShellReflectionUtils . PSVersion ;
26
26
27
27
/// <summary>
28
- /// Get's the Edition of PowerShell being used.
28
+ /// Gets the Edition of PowerShell being used.
29
29
/// </summary>
30
30
public static string PSEdition { get ; } = PowerShellReflectionUtils . PSEdition ;
31
31
32
+ /// <summary>
33
+ /// Gets the string of the PSVersion including prerelease tags if it applies.
34
+ /// </summary>
35
+ public static string PSVersionString { get ; } = PowerShellReflectionUtils . PSVersionString ;
36
+
32
37
/// <summary>
33
38
/// True if we are running in Windows PowerShell, false otherwise.
34
39
/// </summary>
@@ -49,8 +54,16 @@ internal static class PowerShellReflectionUtils
49
54
{
50
55
51
56
private static readonly Type s_psVersionInfoType = typeof ( System . Management . Automation . Runspaces . Runspace ) . Assembly . GetType ( "System.Management.Automation.PSVersionInfo" ) ;
57
+
58
+ // This property is a Version type in PowerShell. It's existed since 5.1, but it was only made public in 6.2.
52
59
private static readonly PropertyInfo s_psVersionProperty = s_psVersionInfoType
53
60
. GetProperty ( "PSVersion" , BindingFlags . NonPublic | BindingFlags . Public | BindingFlags . Static ) ;
61
+
62
+ // This property is a SemanticVersion in PowerShell that contains the prerelease tag as well.
63
+ // It was added in 6.2 so we can't depend on it for anything before.
64
+ private static readonly PropertyInfo s_psCurrentVersionProperty = s_psVersionInfoType
65
+ . GetProperty ( "PSCurrentVersion" , BindingFlags . NonPublic | BindingFlags . Public | BindingFlags . Static ) ;
66
+
54
67
private static readonly PropertyInfo s_psEditionProperty = s_psVersionInfoType
55
68
. GetProperty ( "PSEdition" , BindingFlags . NonPublic | BindingFlags . Public | BindingFlags . Static ) ;
56
69
@@ -64,5 +77,12 @@ internal static class PowerShellReflectionUtils
64
77
/// Get's the Edition of PowerShell being used.
65
78
/// </summary>
66
79
public static string PSEdition { get ; } = s_psEditionProperty . GetValue ( null ) as string ;
80
+
81
+ /// <summary>
82
+ /// Gets the stringified version of PowerShell including prerelease tags if it applies.
83
+ /// </summary>
84
+ public static string PSVersionString { get ; } = s_psCurrentVersionProperty != null
85
+ ? s_psCurrentVersionProperty . GetValue ( null ) . ToString ( )
86
+ : s_psVersionProperty . GetValue ( null ) . ToString ( ) ;
67
87
}
68
88
}
0 commit comments