PowerShell 7.5 的新功能
PowerShell 7.5.0 包含下列功能、更新和重大變更。 PowerShell 7.5 是以 .NET 9.0.1 GA 版本為基礎。
如需變更的完整清單,請參閱 GitHub 存放庫中的 CHANGELOG 。 如需 .NET 9 的詳細資訊,請參閱 [.NET 9 的新功能][07]。
重大變更
- 修正
-OlderThan
使用-NewerThan
和Test-Path
PathType
日期範圍時的參數 (#20942) (感謝@ArmaanMcleod!)-
-OlderThan
先前在一起指定時會被忽略
-
- 將預設值變更
New-FileCatalog -CatalogVersion
為 2 (#20428) (感謝 @ThomasNieto!) - 封鎖從受限制遠程會話中的網路位置取得協助 (#20593)
- Windows 安裝程式現在會記住所使用的安裝選項,並使用這些選項來初始化下一個安裝的選項 (#20420) (感謝 @reduckted!)
-
ConvertTo-Json
現在串行化BigInteger
為數位 (#21000) (感謝 @jborean93!) - .NET 9 已移除
BinaryFormatter
的實作,導致Out-GridView
cmdlet 中的回歸錯誤。Out-GridView
的搜尋功能無法在PowerShell 7.5中使用。 此問題會在 問題 #24749中追蹤。
已更新的模組
PowerShell 7.5.0 包含下列更新的模組:
- Microsoft.PowerShell.PSResourceGet v1.1.0
- PSReadLine v2.3.6
Tab 鍵自動完成改善
許多人感謝 @ArmaanMcleod 和其他人的所有工作,以改善索引標籤完成。
- 當無法從安全表達式擷取哈希表索引鍵/值時回到類型推斷 (#21184) (感謝 @MartinGC94!)
- 修正針對 (#21223) 進行型別推斷
$_
時的回歸 (感謝@MartinGC94!) - 展開
~
至$home
具有索引標籤完成的 Windows (#21529) - 宣告參數名稱和類別成員時請勿完成 (#21182) (感謝 @MartinGC94!)
- 當索引標籤完成類型名稱時,防止後援檔案完成 (#20084) (感謝 @MartinGC94)
- 將自變數完成器新增至
-Version
forSet-StrictMode
(#20554) (感謝 @ArmaanMcleod!) - 為和 重構新增
-Verb
自變數完成器Get-Verb
/Get-Command
(Get-Verb
) (感謝@ArmaanMcleod) - 為
-Verb
(#20415) 新增Start-Process
自變數完成器 (感謝@ArmaanMcleod) - 新增
-Scope
、*-Variable
和*-Alias
命令的*-PSDrive
自變數完成器 (#20451) (感謝@ArmaanMcleod) - 新增
-Module
命令的Save-Help
/Update-Help
完成 (#20678) (感謝 @ArmaanMcleod)
新的 Cmdlet
- 新增
ConvertTo-CliXml
和ConvertFrom-CliXml
Cmdlet (#21063) (感謝 @ArmaanMcleod!)
Web Cmdlet 改善
- 修正以允許
-PassThru
和-Outfile
一起運作 (#24086) - 在
OutFile
中新增WebResponseObject
属性 (#24047) - 在
Invoke-WebRequest -OutFile -Verbose
中顯示檔名 (#24041) - 修正指定但不是時
-Body
WebCmdlet (#23952ContentType
感謝@CarloToso!) - 修正
Invoke-WebRequest
以在指定時-Resume
報告正確的大小 (#20207) (感謝 @LNKLEO!) - 修正 Web Cmdlet 以允許
WinForm
應用程式正常運作 (#20606)
其他 Cmdlet 改善
- 啟用
-NoRestart
以使用Register-PSSessionConfiguration
(#23891) - 將 和
IgnoreComments
選項新增AllowTrailingCommas
至Test-Json
Cmdlet (#23817) (感謝@ArmaanMcleod!) - Get-Help 可能會報告屬性為
ValueFromRemainingArguments
管線功能的參數(#23871) - 將的類型
LineNumber
變更為ulong
inSelect-String
(#24075) (感謝 @Snowman-25!) -
Get-Process
:移除 (#21302) 的-IncludeUserName
系統管理員需求 (感謝@jborean93!) - 修正
Test-Path -IsValid
以檢查無效的路徑和檔案名字符 (#21358) - 新增
RecommendedAction
至ConciseView
錯誤報告 (#20826) (感謝 @JustinGrote!) - 已新增 Cmdlet 的
Remove-Item
進度列 (#20778) (感謝 @ArmaanMcleod!) - 因 .NET 8 變更而修正
Test-Connection
(#20369) - 修正
Get-Service
非終止錯誤訊息以包含類別 (#20276) - 將和
-Empty
參數新增-InputObject
至New-Guid
(#20014) (感謝@CarloToso!) - 將別名
r
新增至-Recurse
命令的參數Get-ChildItem
(#20100) (感謝 @kilasuit!) - 將 新增
LP
至LiteralPath
函式仍遺失的別名 (#20820) - 將隱含本地化後援新增至
Import-LocalizedData
(#19896) (感謝 @chrisdent-de!) - 當格式化 所
Aliases
傳回參數的說明內容時,新增Get-Help
至所顯示的屬性 (#20994) - 新增
HelpUri
至Remove-Service
(#20476) - 修正 SCCM 提供者的完成當機 (#20815, #20919, #20915) (感謝 @MartinGC94!)
- 修正 和 一起使用時的
Get-Content
-Tail 0
回歸-Wait
(#20734) (感謝@CarloToso!) - 修正
Start-Process -PassThru
以確定傳回ExitCode
的物件可以存取 屬性 (Process
) (感謝@CodeCyclone!) - 修正
Group-Object
以將其輸出使用目前的文化特性 (#20608) - 使用插補字串修正
Group-Object
輸出 (#20745) (感謝 @mawosoft!) - 修正網络的
DisplayRoot
轉譯PSDrive
(#20793) - 修正
Copy-Item
進度,只在複製所有檔案時顯示已完成 (#20517) - 修正 UNC 路徑完成回歸 (#20419) (感謝 @MartinGC94!)
- 如果傳遞無效
-ExecutionPolicy
,pwsh
則回報錯誤 (#20460) - 新增 WinGetCommandNotFound 和 CompletionPredictor 模組以追蹤使用量 (#21040)
- 將 DateKind 參數新增至
ConvertFrom-Json
(#20925) (感謝@jborean93!) - 將 DirectoryInfo 新增至 New-Item 的 OutputType (#21126) (感謝@MartinGC94!)
- 修正
Get-Error
陣列值的串行化 (#21085) (感謝 @jborean93!) - 修正
Test-ModuleManifest
,使其可以使用 UNC 路徑 (#24115) - 修正
Get-TypeData
以立即寫入管線,而不是先收集數據 (#24236) (感謝@MartinGC94) - 將 參數新增
-Force
至Resolve-Path
和Convert-Path
Cmdlet 以支援通配符隱藏檔案 #20981 (感謝 @ArmaanMcleod!)
引擎改善
- 明確啟動和停止 ANSI 錯誤色彩 (#24065) (感謝 @JustinGrote!)
- 改善泛型方法的 .NET 多載定義 (#21326) (感謝 @jborean93!)
-
+=
將集合的作業優化為物件陣列時 (#23901) (感謝@jborean93!) - 新增遙測以在匯入模組時檢查特定標籤 (#20371)
- 將與新增
PSAdapter
ConsoleGuiTools
至模組載入遙測允許清單 (#20641) - 新增 WinGet 模組以追蹤使用量 (#21040)
- 記錄 WDAC ETW 事件時,請確定檔名不是 Null (#20910) (感謝 @jborean93!)
- 修正 WDAC 記錄功能引進的四個回歸 (#20913)
- 當輸入、輸出和錯誤未重新導向時,請保留未設定的輸入、輸出和錯誤句柄 (#20853)
- 修正隱含遠端 Proxy Cmdlet 以針對一般參數採取行動 (#20367)
- 找不到模組時,請在錯誤訊息中包含模組版本 (#20144) (感謝@ArmaanMcleod!)
- 修正
unixmode
以處理setuid
檔案sticky
不是可執行檔案時 #20366) - 修正使用元件來使用 Path.Combine 建構元件路徑時 (#21169)
- 驗證在語意檢查期間使用命名空間的值,以防止宣告無效的命名空間 (#21162)
- 在 PATH 前面
$PSHome
特別處理全域工具 (#24228)
實驗性功能
下列實驗性功能已轉換成 PowerShell 7.5-rc.1 中的主流功能:
PowerShell 7.5-rc.1 包含下列實驗性功能:
- PSRedirectToVariable - 允許重新導向至變數 (#20381)
- PSNativeWindowsTildeExpansion - 為 Windows 原生可執行檔新增磚擴充 (#20402) (感謝@domsleee!)
-
PSSerializeJSONLongEnumAsNumber -
ConvertTo-Json
現在會將大型列舉視為數位 (#20999) (感謝@jborean93!)
效能改善
PowerShell 7.5-rc.1 包含來自 的 @jborean93,這提高了物件數組在進行 +=
作業時的效能。
下列範例測量將元素新增至陣列的不同方法的效能。
$tests = @{
'Direct Assignment' = {
param($count)
$result = foreach($i in 1..$count) {
$i
}
}
'List<T>.Add(T)' = {
param($count)
$result = [Collections.Generic.List[int]]::new()
foreach($i in 1..$count) {
$result.Add($i)
}
}
'Array+= Operator' = {
param($count)
$result = @()
foreach($i in 1..$count) {
$result += $i
}
}
}
5kb, 10kb | ForEach-Object {
$groupResult = foreach($test in $tests.GetEnumerator()) {
$ms = (Measure-Command { & $test.Value -Count $_ }).TotalMilliseconds
[pscustomobject]@{
CollectionSize = $_
Test = $test.Key
TotalMilliseconds = [math]::Round($ms, 2)
}
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
}
$groupResult = $groupResult | Sort-Object TotalMilliseconds
$groupResult | Select-Object *, @{
Name = 'RelativeSpeed'
Expression = {
$relativeSpeed = $_.TotalMilliseconds / $groupResult[0].TotalMilliseconds
$speed = [math]::Round($relativeSpeed, 2).ToString() + 'x'
if ($speed -eq '1x') { $speed } else { $speed + ' slower' }
}
} | Format-Table -AutoSize
}
當您在 PowerShell 7.4.6 中執行腳本時,您會看到使用 +=
運算子是最慢的方法。
CollectionSize Test TotalMilliseconds RelativeSpeed
-------------- ---- ----------------- -------------
5120 Direct Assignment 4.17 1x
5120 List<T>.Add(T) 90.79 21.77x slower
5120 Array+= Operator 342.58 82.15x slower
CollectionSize Test TotalMilliseconds RelativeSpeed
-------------- ---- ----------------- -------------
10240 Direct Assignment 0.64 1x
10240 List<T>.Add(T) 184.10 287.66x slower
10240 Array+= Operator 1668.13 2606.45x slower
當您在 PowerShell 7.5-rc.1 中執行腳本時,您會看到使用 +=
運算符比 PowerShell 7.4.6 快得多。 現在,它也比使用 List<T>.Add(T)
方法更快。
CollectionSize Test TotalMilliseconds RelativeSpeed
-------------- ---- ----------------- -------------
5120 Direct Assignment 4.71 1x
5120 Array+= Operator 40.42 8.58x slower
5120 List<T>.Add(T) 92.17 19.57x slower
CollectionSize Test TotalMilliseconds RelativeSpeed
-------------- ---- ----------------- -------------
10240 Direct Assignment 1.76 1x
10240 Array+= Operator 104.73 59.51x slower
10240 List<T>.Add(T) 173.00 98.3x slower
[07]: /dotnet/core/whats-new/dotnet-9/overview)