PowerShell 相較于CMD有什么優(yōu)點?
PowerShell 從 Windows7 時代開始內置于 Windows 系統(tǒng)當中,可以看作是微軟對 cmd 的大升級,目前兩者并存于 Windows 系統(tǒng)中。
與接受和返回文本的大多數 shell 不同,Windows PowerShell 是在 .NET Framework 公共語言運行時 (CLR) 和 .NET Framework 的基礎上構建的,它接受和返回 .NET Framework 對象。環(huán)境中的這一根本更改帶來了管理和配置 Windows 的全新工具和方法。只要你足夠了解.net,你可以使用她做任何你想做的事情。
1. PowerShell 更加 “powerful”
微軟起“PowerShell ”這個名字雖然有吹牛逼的嫌疑(我以前也這樣想),但從事實來看并非如此。PowerShell 的強大程度分分鐘秒殺 Linux 上的 bash,更不要說飽受詬病的 cmd 了。
2. PowerShell 全面支持面向對象
PowerShell 背后依靠的是一套完整的 .NET 編程體系,其腳本更容易編寫且穩(wěn)健性大大提升。反過來看 cmd,那些完全由各種命令堆砌而成的一條條指令簡直就是“小打小鬧”。
3. 從 cmd 遷移到 PowerShell 成本幾乎為零
如果你對老朋友 cmd 充滿情懷,無法舍棄,完全無妨!因為 PowerShell 可以看作 cmd 的超集,所有的常用命令諸如dir, cd, ipconfig等在 PowerShell 中都能直接使用。但背后的實現(xiàn)方式是完全不同的,PowerShell 基于完全的面向對象,它通過給函數和對象“起別名”的方式來支持這些舊的命令。
4. 誘人的管道操作
管道操作的靈感來自 Linux 的 shell,但由于 PowerShell 將一切都包裝成為對象,而不是直接處理字符串,因此其管道操作的靈活程度遠在 Linux 的 shell 之上。
例如:
PS C:UsersHaley> ls | sort -Descending Name | Format-Table Name,Mode Name 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:UsersHaley> 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:UsersHaley> 1 + 1 2 PS C:UsersHaley> 3 * 5 15 PS C:UsersHaley> 0xab 171 PS C:UsersHaley> 1kb 1024 PS C:UsersHaley> 1mb / 1kb 1024
7. 良好的腳本編程體驗
PowerShell 腳本的語法與高級編程語言非常相近,例如其分支語句if(…){} else{}、循環(huán)語句for(…){}與 C 語言別無二致,因而大大提高了編程體驗。
8. 擁有豐富的字符串操作對象和函數,全面支持正則表達式
字符串處理是 shell 腳本的主要任務,正則表達式的重要性也無需多說。
9. 支持重定向,讀寫文件易如反掌
PowerShell 原生支持將結果導出到html, csv, xml等文件,也可以通過重定向從文件中讀取內容。
例如:
PS C:UsersHaley> ls | Select-Object Mode,Name | Export-Csv ~/desktop/test.csv
這樣就把當前目錄下的所有 文件名-讀寫權限 保存到了一個 csv 文件中。
10. 支持 Debug
雖然這個功能很少用到,但支持 debug 是走向完備編程語言不可或缺的一項功能。