你應該選擇 Powershell 的10個理由(拋棄 cmd)
Powershell 從 Windows7 時代開始內置于 Windows 系統(tǒng)當中,可以看作是微軟對 cmd 的大升級,目前兩者并存于 Windows 系統(tǒng)中。
首先強烈推薦一個 powershell 學習網站:
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 基于完全的面向對象,它通過給函數(shù)和對象“起別名”的方式來支持這些舊的命令。
4. 誘人的管道操作
管道操作的靈感來自 Linux 的 shell,但由于 powershell 將一切都包裝成為對象,而不是直接處理字符串,因此其管道操作的靈活程度遠在 Linux 的 shell 之上。
例如:
PS C:\Users\Haley> 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. 絕對完備的幫助文檔
任何函數(shù)與對象都能夠通過help *命令來查看其幫助文檔(準確來說應該是Get-Help函數(shù),這是更加“面向對象”化的命名方式,而help是它的別名),如果看不明白,加上-examples參數(shù)會有應用實例,如果仍看不明白,加上-online參數(shù)會打開完整的在線幫助文檔,不得不佩服,微軟的一條龍服務做的很到位。
例如,關于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. 支持基本的數(shù)學運算和數(shù)組操作(加減乘除模),打開就能當計算器用
PS C:\Users\Haley> 1 + 1 2 PS C:\Users\Haley> 3 * 5 15 PS C:\Users\Haley> 0xab 171 PS C:\Users\Haley> 1kb 1024 PS C:\Users\Haley> 1mb / 1kb 1024
7. 良好的腳本編程體驗
powershell 腳本的語法與高級編程語言非常相近,例如其分支語句if(...){} else{}、循環(huán)語句for(...){}與 C 語言別無二致,因而大大提高了編程體驗。
8. 擁有豐富的字符串操作對象和函數(shù),全面支持正則表達式
字符串處理是 shell 腳本的主要任務,正則表達式的重要性也無需多說。
9. 支持重定向,讀寫文件易如反掌
powershell 原生支持將結果導出到html, csv, xml等文件,也可以通過重定向從文件中讀取內容。
例如:
PS C:\Users\Haley> ls | Select-Object Mode,Name | Export-Csv ~/desktop/test.csv
這樣就把當前目錄下的所有 文件名-讀寫權限 保存到了一個 csv 文件中。
10. 支持 Debug
雖然這個功能很少用到,但支持 debug 是走向完備編程語言不可或缺的一項功能。
相關文章
Powershell直接腳本時出現(xiàn)無法加載文件因為禁止執(zhí)行腳本
Powershell直接腳本時出現(xiàn)無法加載文件因為在此系統(tǒng)中禁止執(zhí)行腳本,有關此問題的解決方法如下2014-08-08Powershell在一個會話中只允許執(zhí)行指定命令的方法
這篇文章主要介紹了Powershell在一個會話中只允許執(zhí)行指定命令的方法,使用本文的技巧可以達到控制權限的功能,需要的朋友可以參考下2014-11-11PowerShell中使用Out-File把字符串或運行結果保存到文件的方法
這篇文章主要介紹了PowerShell中使用Out-File把字符串或運行結果保存到文件的方法,重點在于Out-File命令的使用方法上,需要的朋友可以參考下2014-08-08