PowerShell
這本書旨在涵蓋 Windows PowerShell 的使用,它是一個基於 .NET 的面向物件的命令列外殼,旨在取代 Windows 上的批處理指令碼、Visual Basic Script 和其他指令碼技術。 後來的 PowerShell 版本也適用於 Linux。
PowerShell 是一種基於 .NET 的現代 Windows 指令碼技術,它具有完整的程式語言,可以訪問 .NET 類,以及基於物件而不是字元流的管道。 它是 Windows 指令碼主機中透過 cmd.exe 和 VBScript 進行指令碼編寫的繼任者。
PowerShell 2.0 是 Windows 7 和 Windows Server 2008 R2 的一部分。
PowerShell 5.1 與 Windows 10 週年更新一起釋出。
截至 2022 年 6 月,PowerShell 7.2 是最新版本。
您可以在 Tio(線上嘗試)中嘗試使用 PowerShell,在 Linux 環境中。
連結
- 安裝 Windows PowerShell,docs.microsoft.com
- 在 Linux 上安裝 PowerShell,docs.microsoft.com
- Tio PowerShell,tio.run
要開始,您可以瞭解可用 cmdlet 的列表及其別名,並瞭解如何獲取有關它們的幫助。 按 Windows + R,輸入“powershell”,PowerShell 控制檯啟動,然後您可以在其中輸入以下示例程式碼。
獲取幫助的示例
- help
- 輸出有關幫助系統的幫助。
- help *
- 輸出專案列表:別名、cmdlet、幫助主題和其他幫助專案。
- help d
- 輸出以 d 開頭或包含以 d 開頭的單詞的別名、cmdlet 和幫助主題列表。
- help d*
- 輸出以 d 開頭的別名、cmdlet 和幫助主題列表。
- help dir
- 輸出有關具有別名“dir”的 cmdlet 的幫助。
- help dir -full
- 輸出有關具有別名 dir 的 cmdlet 的完整幫助。
- help Get-ChildItem
- 輸出有關該 cmdlet 的幫助。
- help about
- 輸出幫助主題列表,即以“about”開頭的幫助專案。
- help about_Arithmetic_Operators
- 輸出有關算術運算子主題的幫助。
- help about*Ari*
- 輸出包含 Ari 作為子字串的幫助主題列表。
- help *Arith*
- 輸出有關算術運算子主題的幫助,只要它是匹配該模式的唯一幫助專案。
更多示例
- $HOME
- 輸出變數 HOME 的值。
- Variable
- 輸出變數。
- gci env:os
- 輸出環境變數 os,它預設不在變數中。
- $PSVersionTable.PSVersion
- 輸出 PowerShell 版本。
- "Hello".GetType()
- 輸出該值的型別。 關鍵詞:typeof。 GetType() 是字串物件的函式。 啟用反射。
- (7).GetType(); 1.7.GetType(); (1, 2).GetType(); @{"Key" = "Value"}.GetType()
- 輸出更多型別。
- "Hello".gettype()
- 函式名稱與 cmdlet 名稱一樣不區分大小寫。
- Get-ChildItem | Where {! $_.PSIsContainer} | Sort-Object -Property Length -Descending | Select-Object -Property Length,Name
- 輸出當前資料夾中的檔案,按大小降序排序,只輸出文件長度和檔名。 這是物件管道逐個修改物件流的示例。 請注意,Select-Object -Property Length,Name 實際上不會選擇物件,而是構造新的物件,這些物件只包含原始物件的選定屬性。
- Get-ChildItem | Get-Member
- 輸出物件成員,包括屬性和方法。 啟用反射。
- 4, 1.5 | Get-Member
- 輸出列表中物件型別的物件成員,System.Int32 和 System.Double。
- [uint32]4 |Get-Member
- 在獲取物件成員之前,將整數字面量轉換為 UInt32 型別。
- (Get-Content MyFile.txt).Length
- 輸出檔案的行數。 搜尋詞:“wc -l”,Unix 等效項。 簡寫為“ (gc _test.txt).Length”。
- Get-Content myfile.txt | Measure-Object -Word
- 輸出檔案的單詞數。
連結
- 在 Y 分鐘內學習 X,其中 X=powershell,learnxinyminutes.com
PowerShell 線上文件按版本提供。
連結
- Microsoft.PowerShell.Core 參考,docs.microsoft.com
- Microsoft.PowerShell.Core 參考,適用於 PowerShell 7.2,docs.microsoft.com
- Microsoft.PowerShell.Core 參考,適用於 PowerShell 7.1,docs.microsoft.com
- Microsoft.PowerShell.Core 參考,適用於 PowerShell 5.1,docs.microsoft.com
- Microsoft.PowerShell.Core 參考,適用於 PowerShell 5.0,docs.microsoft.com
- Microsoft.PowerShell.Core 參考,適用於 PowerShell 3.0,docs.microsoft.com
查詢 PowerShell 引擎的版本
- $PSVersionTable
連結
- PowerTip:檢查 PowerShell 的版本,devblogs.microsoft.com
PowerShell 支援基於 .NET 支援的算術運算。
示例
- (1 + 2) * 3
- 一個簡單的整數計算。
- (1 + 2) * 3 / 7
- 支援浮點結果。
- $a = 3/2; $a + 1
- 中間結果可以儲存在變數中。
- [Math]::Pow(2, 100); [bigint]::Pow(2, 100)
- 可以使用 Math 和 bigint 進行指數運算; Math 將大數轉換為浮點數。 其他常見的浮點函式可用,例如 Sin、Cos、Sqrt 等。
- 0xFE -band -bnot 0xF
- 支援按位算術運算。 其他運算子包括 -bor、-bxor、-shl 和 -shr。
連結
- about_Arithmetic_Operators,docs.microsoft.com
輸出當前別名。 別名:gal。
示例
- gal
- 列出所有別名。
- gal g*
- 列出所有以 g 開頭的別名。
連結
- Get-Alias,docs.microsoft.com
輸出有關幫助專案(如 cmdlet 或主題)的幫助。 它的別名是 help。 可以使用 Update-Help cmdlet 下載專案的幫助。
示例
- Get-Help
- 輸出有關幫助系統的幫助。
- Get-Help *
- 輸出專案列表:別名、cmdlet、幫助主題和其他幫助專案。
- Get-Help Get-ChildItem
- 獲取有關 Get-ChildItem cmdlet 的幫助。
- Get-Help Get-ChildItem -full
- help dir -full
- 與上面相同,但依賴於 help 是 Get-Help 的別名,dir 是 Get-ChildItem 的別名。
- help *
- 輸出別名、cmdlet、幫助主題和其他幫助專案的列表。
- help d
- 輸出以字母 d 開頭或包含以 d 開頭的單詞的別名、cmdlet 和幫助主題列表。
- help dir
- 輸出有關具有別名“dir”的 cmdlet 的幫助。
- help dir -full
- 輸出有關具有別名 dir 的 cmdlet 的完整幫助。
- help Get-ChildItem
- 輸出有關該 cmdlet 的幫助。
- help about
- 輸出幫助主題列表。
- help about_Arithmetic_Operators
- 輸出有關算術運算子主題的幫助。
- help about*Ari*
- 輸出包含 Ari 作為子字串的幫助主題列表。
- help *Arith*
- 輸出有關算術運算子主題的幫助,只要它是匹配該模式的唯一幫助專案。
連結
在輸入中查詢字串模式,充當行的過濾器;與 grep 相關。
示例
- "Cat", "Cat2", "Dog" | Select-String -Pattern "Cat"
連結
- Select-String,docs.microsoft.com
- Select-String,ss64.com
將幫助項或一組幫助項下載到計算機。需要管理員許可權。
示例
- Update-Help
- Update-Help -Module Microsoft.PowerShell.Core
連結
- Update-Help,docs.microsoft.com
- Update-Help,ss64.com
- 安裝 Windows PowerShell,docs.microsoft.com
- PowerShell 文件,docs.microsoft.com
- Microsoft.PowerShell.Core 參考,docs.microsoft.com
- Windows PowerShell 生存指南,social.technet.microsoft.com
- Windows PowerShell 命令的 A-Z 索引,ss64.com
- Windows PowerShell 操作指南和示例,ss64.com
- 在 Y 分鐘內學習 X,其中 X=powershell,learnxinyminutes.com
- PowerShell,wikipedia.org
- PowerShell,wikiversity.org
- Powershell 速查表,由 James Hedges 提供,gitlab.com
- 類別:PowerShell,rosettacode.org - 許多工的示例指令碼