跨版本的 PowerShell 安裝文件夾和腳本文件擴展名 (PowerShell installation folder and script filename extension across versions)


問題描述

跨版本的 PowerShell 安裝文件夾和腳本文件擴展名 (PowerShell installation folder and script filename extension across versions)

維基百科說 PowerShell 2.0 隨 Windows 7 一起發布;所以當我使用 Windows 7 時,我顯然在使用那個版本。

但這引發了兩個問題:

  1. 為什麼 PowerShell 的文件夾 C:\Windows\ System32\WindowsPowerShell\v1.0 後面的 v1.0 實際上應該是 v2.0
  2. 為什麼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

(by pokeJaredParmklement0)

參考文件

  1. PowerShell installation folder and script filename extension across versions (CC BY‑SA 3.0/4.0)

#powershell #versioning






相關問題

玩弄 C# 加密 (Playing around with C# encryption)

查找具有特定 startname 的特定服務 (Finding specific services with specific startname)

試圖從事件日誌中獲取數據到電子郵件中的 html (Trying to get data from event logs into html in email)

如何通過powershell獲取請求的authtoken (How to obtain authtoken for request via powershell)

如何在錯誤時繼續使用 &$var 調用可執行文件 (How to continue on error a call to an executable using &$var)

在同一範圍內從 c# 調用多個 powershell 命令 (Invoke multiple powershell commands from c# on the same scope)

如何在 Powershell 二進制模塊(.Net Standard 和 Nuget)中處理公共和私有依賴項和打包 (How to handle public and private dependencies and packaging in Powershell binary module (.Net Standard & Nuget))

如何使用參數(描述空白)獲取廣告中的所有 OU - 沒有描述? (How to get all OU in Ad with parameter (description blank ) - without description?)

遠程服務器返回錯誤:(400) 錯誤請求。在 C:\Program Files\WindowsPowerShell\Modules\CosmosDB\3.1.0.293\CosmosDB.psm1 (The remote server returned an error: (400) Bad Request. At C:\Program Files\WindowsPowerShell\Modules\CosmosDB\3.1.0.293\CosmosDB.psm1)

Powershell 檢查文件類型 (Powershell check file type)

服務器待重啟 (Server Pending Reboot)

PowerShell 的 Invoke-WebRequest 在 Docker 容器中不起作用 (PowerShell's Invoke-WebRequest not working within a Docker Container)







留言討論