問題描述
跨版本的 PowerShell 安裝文件夾和腳本文件擴展名 (PowerShell installation folder and script filename extension across versions)
維基百科說 PowerShell 2.0 隨 Windows 7 一起發布;所以當我使用 Windows 7 時,我顯然在使用那個版本。
但這引發了兩個問題:
- 為什麼 PowerShell 的文件夾
C:\Windows\ System32\WindowsPowerShell\v1.0
後面的v1.0
實際上應該是v2.0
? - 為什麼PowerShell 腳本擴展
.ps1
? 版本更改時該擴展是否會增加?
## 參考解法 #### 方法 1:
This is simply the result of a choice made by the Powershell team. They decided to keep both the 1.0 directory and the .ps1 extension for V2 of powershell.
The best way to actually check the version of Powershell is to use the expression $PSVersionTable.PSVersion
C:\Users\jaredpar> $PSVersionTable.PSVersion
Major Minor Build Revision
‑‑‑‑‑ ‑‑‑‑‑ ‑‑‑‑‑ ‑‑‑‑‑‑‑‑
2 0 ‑1 ‑1
方法 2:
This is an ancient blog post (2007), but it still applies; in short:
As long as new PowerShell versions remain backward‑compatible, they will replace earlier versions:
The installation location, reflected in
$PSHOME
‑$env:systemroot\System32\WindowsPowerShell\v1.0
‑ will remain the same.The filename extension ‑
.ps1
‑ will remain the same.
Scripts created for an earlier version will continue to run.
To mark a script as requiring version
<n>
at a minimum , use#requires ‑version <n>
at the top of the script (technically, it can be placed anywhere in the script, but it makes sense sense to place it at the top).
In Windows PowerShell, backward compatibility has been maintained since v1 (current is v5.1 as of this writing ‑ likely the last major version; see below), so both the installation location and the filename extension have remained the same.
However, all future effort will go toward the separate, cross‑platform PowerShell Core edition, which ‑ as of v6.2.0 ‑ is largely backward‑compatible with Windows PowerShell, though its use of .NET Core as the foundation means that certain Windows‑specific technologies are fundamentally unavailable ‑ see this blog post.
To get the current session's PowerShell version:
PS> [string] $PSVersionTable.PSVersion
5.1.14393.693 # PSv5.1 example
More generally, hashtable $PSVersionTable
, introduced in v2, contains several pieces of version information, (incompletely) described in Get‑Help about_Automatic_Variables
; WinPS below refers to Windows PowerShell, whereas PSCore refers to PowerShell Core:
Shared properties:
Name Value
‑‑‑‑ ‑‑‑‑‑
PSVersion 5.1.14393.693 # The PowerShell version.
PSEdition Desktop # 'Desktop'=WinPS; 'Core'=PSCore
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} # array of compatible versions
WSManStackVersion 3.0 # WS‑Management (WinRM) version
PSRemotingProtocolVersion 2.3 # remoting‑protocol version
SerializationVersion 1.1.0.1 # serialization‑protocol version
Additional properties exclusive to WinPS:
BuildVersion 10.0.14393.693 # ?? Highest supported OS major.minor version, seemingly followed by the build.revision PS version.
CLRVersion 4.0.30319.42000 # The .NET Framework CLR version
Additional properties exclusive to PSCore:
GitCommitId 6.2.0 # The Git commit ID reflecting an RTM tag (e.g., 6.2.0), release tag (e.g., 6.2.0‑rc.1) or a specific non‑release commit (e.g., 6.2.0‑preview.4‑108‑g5d54f1aa3871a826409496437e25856dc263ccc4)
OS Microsoft Windows 10.0.17134 # [System.Runtime.InteropServices.RuntimeInformation]::OSDescription
Platform Win32NT # [System.Environment]::OSVersion.Platform