Powershell 從 Windows7 時代開始內置于 Windows 系統當中,可以看作是微軟對 cmd 的大升級,目前兩者并存于 Windows 系統中。
首先強烈推薦一個 powershell 學習網站:
powershell在線教程
1. powershell 更加 “powerful”
微軟起“powershell”這個名字雖然有吹牛逼的嫌疑(我以前也這樣想),但從事實來看并非如此。powershell 的強大程度分分鐘秒殺 Linux 上的 bash,更不要說飽受詬病的 cmd 了。(無意打廣告)
2. powershell 全面支持面向對象
powershell 背后依靠的是一套完整的 .NET 編程體系,其腳本更容易編寫且穩健性大大提升。反過來看 cmd,那些完全由各種命令堆砌而成的一條條指令簡直就是“小打小鬧”。
3. 從 cmd 遷移到 powershell 成本幾乎為零
如果你對老朋友 cmd 充滿情懷,無法舍棄,完全無妨!因為 powershell 可以看作 cmd 的超集,所有的常用命令諸如dir, cd, ipconfig等在 powershell 中都能直接使用。但背后的實現方式是完全不同的,powershell 基于完全的面向對象,它通過給函數和對象“起別名”的方式來支持這些舊的命令。
4. 誘人的管道操作
管道操作的靈感來自 Linux 的 shell,但由于 powershell 將一切都包裝成為對象,而不是直接處理字符串,因此其管道操作的靈活程度遠在 Linux 的 shell 之上。
例如:
PS C:/Users/Haley> ls | sort -Descending Name | Format-Table Name,ModeName Mode---- ----VirtualBox VMs d-----Videos d-r---Searches d-r---Saved Games d-r---Pictures d-r---OneDrive d-r---Music d-r---Links d-r---
這條命令列出當前路徑下的所有文件,按照名稱降序排序,并以表格的形式輸出,且只顯示Name和Mode兩個字段。
5. 絕對完備的幫助文檔
任何函數與對象都能夠通過help *命令來查看其幫助文檔(準確來說應該是Get-Help函數,這是更加“面向對象”化的命名方式,而help是它的別名),如果看不明白,加上-examples參數會有應用實例,如果仍看不明白,加上-online參數會打開完整的在線幫助文檔,不得不佩服,微軟的一條龍服務做的很到位。
例如,關于ls的幫助文檔如下:
PS C:/Users/Haley> help ls名稱 Get-ChildItem摘要 Gets the items and child items in one or more specified locations.語法 Get-ChildItem [[-Filter] <String>] [-Attributes {ReadOnly | Hidden | System | Directory | Archive | Device | Normal | Temporary | SparseFile | ReparsePoint | Compressed | Offline | NotContentIndexed | Encrypted | IntegrityStream | NoScrubData}] [-Depth <UInt32>] [-Directory] [-Exclude <String[]>] [-File] [-Force] [-Hidden] [-Include <String[]> ] -LiteralPath <String[]> [-Name] [-ReadOnly] [-Recurse] [-System] [-UseTransaction] [<CommonParameters>] Get-ChildItem [[-Path] <String[]>] [[-Filter] <String>] [-Attributes {ReadOnly | Hidden | System | Directory | Arch ive | Device | Normal | Temporary | SparseFile | ReparsePoint | Compressed | Offline | NotContentIndexed | Encrypte d | IntegrityStream | NoScrubData}] [-Depth <UInt32>] [-Directory] [-Exclude <String[]>] [-File] [-Force] [-Hidden] [-Include <String[]>] [-Name] [-ReadOnly] [-Recurse] [-System] [-UseTransaction] [<CommonParameters>]說明 The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child cont ainers. A location can be a file system location, such as a directory, or a location exposed by a different Windows PowerSh ell provider, such as a registry hive or a certificate store.相關鏈接 Online Version: http://go.microsoft.com/fwlink/?LinkId=821580 Get-Item Get-Location Get-Process Get-PSProvider備注 若要查看示例,請鍵入: "get-help Get-ChildItem -examples". 有關詳細信息,請鍵入: "get-help Get-ChildItem -detailed". 若要獲取技術信息,請鍵入: "get-help Get-ChildItem -full". 有關在線幫助,請鍵入: "get-help Get-ChildItem -online"
6. 支持基本的數學運算和數組操作(加減乘除模),打開就能當計算器用
PS C:/Users/Haley> 1 + 12PS C:/Users/Haley> 3 * 515PS C:/Users/Haley> 0xab171PS C:/Users/Haley> 1kb1024PS C:/Users/Haley> 1mb / 1kb1024
7. 良好的腳本編程體驗
powershell 腳本的語法與高級編程語言非常相近,例如其分支語句if(...){} else{}、循環語句for(...){}與 C 語言別無二致,因而大大提高了編程體驗。
8. 擁有豐富的字符串操作對象和函數,全面支持正則表達式
字符串處理是 shell 腳本的主要任務,正則表達式的重要性也無需多說。
9. 支持重定向,讀寫文件易如反掌
powershell 原生支持將結果導出到html, csv, xml等文件,也可以通過重定向從文件中讀取內容。
例如:
PS C:/Users/Haley> ls | Select-Object Mode,Name | Export-Csv ~/desktop/test.csv
這樣就把當前目錄下的所有 文件名-讀寫權限 保存到了一個 csv 文件中。
10. 支持 Debug
雖然這個功能很少用到,但支持 debug 是走向完備編程語言不可或缺的一項功能。
新聞熱點
疑難解答
圖片精選