PowerShell

PowerShell
在Windows Terminal内运行的PowerShell 7
编程范型命令式、管道、面向对象函数式反射式
設計者杰弗里·史诺威英语Jeffrey Snover(Jeffrey Snover)
布鲁斯·帕耶特(Bruce Payette)
詹姆斯·特鲁赫(James Truher) (et al.)
實作者Microsoft
发行时间2006年11月14日,​17年前​(2006-11-14
当前版本
  • 7.4.2 (2024年4月11日;穩定版本)[1]
編輯維基數據鏈接
型態系統强类型类型安全类型推论动态类型
系统平台PowerShell: .NET
Windows PowerShell: .NET框架
操作系统Windows 7及其后续版本、Linux、macOS
許可證MIT許可證[2](但Windows组件仍然是专有的
文件扩展名
  • .ps1 (脚本文件)
  • .ps1xml(XML文档)
  • .psc1(控制台文件)
  • .psd1(数据文件)
  • .psm1(脚本模块)
  • .pssc(会话配置文件)
  • .cdxml (Cmdlet定义的XML文档)
網站microsoft.com/powershell 編輯維基數據鏈接
啟發語言
KshPerlC#CL英语IBM i Control LanguageDCL英语DIGITAL Command LanguageSQLTclTk[3]ChefPuppet

PowerShell(包括Windows PowerShellPowerShell Core)是微軟公司开发的任务自动化和組態管理架構,由在.NET Framework和后来的.NET上构建的命令行界面殼層相关脚本语言组成,最初僅僅是Windows元件,后于2016年8月18日开源并提供跨平台支持。[4]

在PowerShell中,管理任务通常由cmdlets(发音为command-lets)执行,这是执行特定操作的专用.NET类。可以将cmdlet集合至脚本、可执行文件(一般是独立应用程序)中,或通过常规.NET类(或WMI / COM对象)实例化。[5][6]

源起與目的

UNIX系統一直有著功能強大的殼程式(shell),Windows PowerShell的誕生就是要提供功能相當於UNIX系統的命令列殼程式(例如:shbashcsh),同時也內建腳本語言以及輔助腳本程式的工具。

程式特性

  • 一致性的設計讓所有工具和系統資料的使用語法、命名原則都相同。
  • 腳本語言簡單易學,而且能支援現有的腳本程式和命令列工具。
  • 內含129種稱為cmdlet的標準工具,可用來處理常見的系統管理工作。
  • 具備完整的擴充功能,獨立軟體商或開發者都能很容易的自行擴充。
  • 进程间数据传递内容具有强类型特征。
  • PowerShell Core可以自動檢測字符編碼,自動識別Linux和Windows下的換行符,跨平台运行。

cmdlet是Windows PowerShell的指令,發音唸法為command-let。這相當於DOS或其他殼程式的內建指令,指令名稱的格式都是以連字號(-)隔開的一對動詞名詞,並且通常都是單數名詞;例如線上查詢說明的cmdlet指令為get-help,名稱的動詞部分大致有get、set、add、remove等等(字母都不分大小寫)。

Windows PowerShell ISE是Windows PowerShell的主机应用程序。在此程序中,可以在单个Windows GUI中运行命令、编辑与测试脚本。此程序具有多行编辑、Tab补齐、上下文相关帮助、语法着色、选择性执行等功能,而且还支持从右到左的书写顺序等功能。

技術基礎

Windows PowerShell是以.NET Framework技術為基礎,並且與現有的WSH保持回溯相容,因此它的腳本程式不僅能存取.NET CLR,也能使用現有的COM技術。同時也包含了數種系統管理工具、簡易且一致的語法,常見如登錄資料庫、WMI。Exchange Server 2007以及System Center Operations Manager 2007等伺服器軟體都將內建Windows PowerShell。

範例

  • 停止所有目前執行中的以"p"字元開頭命名的程式:
get-process p* | stop-process
  • 停止所有目前執行中的所有使用大於1000MB記憶體的程式:
get-process | where { $_.WS -gt 1000MB } | stop-process
  • 計算一個目錄下檔案內的位元組大小:
get-childitem | measure-object -property length -sum
  • 將"hello, world!"字串轉為英文大寫字元,成為"HELLO, WORLD!":
"hello, world!".ToUpper()
  • 訂閱一個指定的RSS Feed並顯示它最近8個主題:
$rssUrl = "http://blogs.msdn.com/powershell/rss.aspx"
$blog = [xml](new-object System.Net.WebClient).DownloadString($rssUrl)
$blog.rss.channel.item | select title -first 8

命令提示字元比较

PowerShell命令行與其他命令行解釋器的內部和外部命令的比較
PowerShell(命令行) PowerShell(別名) 命令提示符 Unix shell 描述
Get-ChildItem gci, dir, ls dir ls 列出目前或指定資料夾中的所有檔案和資料夾
Test-Connection[a] ping ping ping 從目前電腦向指定電腦傳送Ping,或指示另一台電腦這樣做
Get-Content gc, type, cat type英语TYPE (DOS command) cat 取得檔案內容
Get-Command gcm help英语help (command) type英语type (Unix), which英语which (command), compgen 列出可用的命令
Get-Help help, man help英语help (command) apropos英语apropos (Unix), man 在控制台上打印命令的文档
Clear-Host cls, clear cls英语cls (computing) clear 清除屏幕[b]
Copy-Item cpi, copy, cp copy, xcopy英语xcopy, robocopy英语robocopy cp 將檔案和資料夾複製到另一個位置
Move-Item mi, move, mv move英语move (command) mv 將檔案和資料夾移動到新位置
Remove-Item ri, del, erase, rmdir, rd, rm del英语del (command), erase英语del (command), rmdir, rd rm, rmdir 刪除檔案或資料夾
Rename-Item rni, ren, mv ren英语ren (command), rename mv 重命名單個檔案、資料夾、硬連結或符號連結
Get-Location gl, cd, pwd cd pwd 顯示工作路徑(目前資料夾)
Pop-Location popd popd英语pushd and popd popd 將工作路徑更改為最近推送到堆疊上的位置
Push-Location pushd pushd英语pushd and popd pushd 將工作路徑儲存到堆疊中
Set-Location sl, cd, chdir cd, chdir cd 改變工作路徑
Tee-Object tee 不適用 tee 將輸入管道傳輸到檔案或變數,並沿管道傳遞輸入
Write-Output echo, write echo echo 將字符串或其他對像打印到標準串流
Get-Process gps, ps tlist,[c] tasklist英语tasklist[d] ps 列出所有正在執行的處理程序
Stop-Process spps, kill kill英语kill (command),[c] taskkill[d] kill[e] 停止正在執行的處理程序
Select-String sls findstr find, grep 打印与模式匹配的行
Set-Variable sv, set set英语Environment variable#DOS env, export, set, setenv 创建或更改环境变量的內容
Invoke-WebRequest iwr, curl, wget[f] curl wget, curl 取得互联网上的网页內容
  1. ^ 尽管外部ping命令仍可用於PowerShell,但“Test-Connection”的输出是一个可以通过编程来检查的结构化对象。
    While the external ping command remains available to PowerShell, Test-Connection's output is a structured object that can be programmatically inspected.[7]
  2. ^ Clear-Host被實現為預定義的PowerShell功能。
  3. ^ 3.0 3.1 Available in Windows NT4, Windows 98 Resource Kit, Windows 2000 Support Tools
  4. ^ 4.0 4.1 Available in Windows XP Professional Edition and later
  5. ^ Also used in UNIX to send a process any signal, the "Terminate" signal is merely the default
  6. ^ curl and wget aliases are absent from PowerShell Core, so as to not interfere with invoking similarly named native commands.

Windows PowerShell 3.0

PowerShell 3.0 与 Windows 8 和 Windows Server 2012 集成。微软还为 Windows 7 的 Service Pack 1、Windows Server 2008 的 Service Pack 1 和 Windows Server 2008 R2 的 Service Pack 1 提供了 PowerShell 3.0。[8][9]

PowerShell 3.0 是一个更大的软件包 Windows Management Framework 3.0 (WMF3) (WMF3) 的一部分。 PowerShell 3.0 是一个更大的软件包 Windows 管理框架 3.0 (WMF3) 的一部分,其中还包含支持远程协作的 WinRM 服务。[10][11]

PowerShell 3.0 的新功能包括:[12][13]

  • 计划任务:可使用 Windows 任务调度程序基础架构计划任务在预定的时间和日期运行。
  • 会话重新连接:会话可以断开并重新连接。远程会话现在能更好地应对临时网络故障。
  • 改进了代码编写:增加了代码自动完成(IntelliSense)和代码片段。PowerShell ISE 允许用户使用对话框填写 PowerShell cmdlet 参数。
  • 授权支持:可将管理任务授权给无权执行此类任务的用户,而无需授予他们永久性的额外权限。
  • 更新帮助:可使用 Update-Help 命令更新帮助文档。

參考文獻

  1. ^ 1.0 1.1 Release 7.4.2. 2024年4月11日 [2024年4月25日]. 
  2. ^ PowerShell LICENSE. [2017-06-10]. (原始内容存档于2022-05-11). 
  3. ^ Snover, Jeffrey. PowerShell and WPF: WTF. Windows PowerShell Blog. Microsoft. 2008-05-25 [2017-06-10]. (原始内容存档于2019-01-08). 
  4. ^ Bright, Peter. PowerShell is Microsoft's latest open source release, coming to Linux, OS X. Ars Technica. Condé Nast. 2016-08-18 [2017-06-10]. (原始内容存档于2022-04-28). 
  5. ^ How Windows PowerShell works. Microsoft Developer Network. Microsoft. [2007-11-27]. (原始内容存档于2008-09-05). 
  6. ^ Truher, Jim. Extend Windows PowerShell With Custom Commands. MSDN Magazine (Microsoft). December 2007 [2017-06-10]. (原始内容存档于2008-10-06). 
  7. ^ Test-Connection. PowerShell documentations. 微软. 9 August 2015 [2019-12-18]. (原始内容存档于2016-10-31). 
  8. ^ PowerShell Version 3 is RTM!. tfl09.blogspot.com. [2023-09-09]. (原始内容存档于2023-09-05). 
  9. ^ WMF 3.0. www.microsoft.com. [2023-09-09]. (原始内容存档于2023-06-19). 
  10. ^ Powershell Version. poshnit.wordpress.com. [2023-09-09]. (原始内容存档于2023-09-05). 
  11. ^ Microsoft has released Windows Management Framework 3.0 - Community Technology Preview. www.ntcompatible.com. [2023-09-09]. (原始内容存档于2023-09-05). 
  12. ^ What Is PowerShell ISE?. www.easeus.com. [2023-09-09]. (原始内容存档于2023-09-05). 
  13. ^ Introducing Windows 8: An Overview for IT Professionals. books.google.com. [2023-09-09]. (原始内容存档于2023-09-05). 

擴展閱讀

  • Oakley, Andy. Monad (AKA PowerShell). O'Reilly Media. 2005. ISBN 0-596-10009-4. 
  • Holmes, Lee. Windows PowerShell Quick Reference. O'Reilly Media. 2006. ISBN 0-596-52813-2. 
  • Holmes, Lee. Windows PowerShell Cookbook. O'Reilly Media. 2007. ISBN 0-596-52849-3. 
  • Watt, Andrew. Professional Windows PowerShell. Wrox Press英语Wrox Press. 2007. ISBN 0-471-94693-1. 
  • Kumaravel, Arul; White, Jon; Naixin Li, Michael; Happell, Scott; Xie, Guohui; Vutukuri, Krishna C. Professional Windows PowerShell Programming: Snapins, Cmdlets, Hosts and Providers. Wrox Press英语Wrox Press. 2008. ISBN 0-470-17393-9. 
  • Kopczynski, Tyson; Handley, Pete; Shaw, Marco. Windows PowerShell Unleashed 2nd. Pearson Education. 2009. ISBN 978-0-672-32988-3. 
  • Jones, Don; Hicks, Jeffery. Windows PowerShell 2.0: TFM 3rd. Sapien Technologies. 2010. ISBN 978-0-9821314-2-8. 
  • Finke, Douglas. Windows PowerShell for Developers. O'Reilly Media. 2012. ISBN 1-4493-2270-0. 
  • Wilson, Ed. Windows PowerShell 3.0 Step by Step. Microsoft Press. 2013. ISBN 978-0-7356-6339-8. 
  • Wilson, Ed. Windows PowerShell Best Practices. Microsoft Press. 2014. ISBN 978-0-7356-6649-8. 

外部連結

微软官网
  • 如何使用 PowerShell 文件 - PowerShell | Microsoft Docs
  • GitHub - PowerShell/PowerShell: PowerShell for every system! (页面存档备份,存于互联网档案馆
  • 易學易用的Windows PowerShell (页面存档备份,存于互联网档案馆
  • MSDN视频教程 (页面存档备份,存于互联网档案馆
其他
管理工具
應用程式
Shell
服務
文件系統
伺服器
架構
安全性
兼容性
API
已經终止
应用程序
其他
圖形介面
音樂與音效
多媒體
Web
資料存取
網路
通訊
  • 訊息API英语Messaging Application Programming Interface
  • 電話API
  • WCF
管理
元件與模型
函式庫
驱动程序
安全性
.NET
软件工廠
  • EFx Factory英语EFx Factory
  • Enterprise Library英语Microsoft Enterprise Library
  • 複合使用者介面英语Composite UI Application Block
  • CCF英语Microsoft Customer Care Framework
  • CSF英语Microsoft Connected Services Framework
行程間通訊
可協助性
  • Active Accessibility英语Microsoft Active Accessibility
  • 使用者介面自動化英语Microsoft UI Automation
文字與多語系支援
  • DirectWrite
  • 文本服务框架英语Text Services Framework
  • 文本对象模型英语Text Object Model
  • 输入法编辑器
  • 語言介面套件英语Language Interface Pack
  • 多語系使用者介面英语Multilingual User Interface
  • Uniscribe
遊戲開發
实现
架構
共同語言基礎架構
CLI支持語言
共同語言1
其他語言2
Core家族3
元件
比較
即將推出
  • "Acropolis"
  • "Jasper"
1微软在Visual Studio.NET Framework中提供了或者将提供支持的语言
2被多數使用者使用,且非教學或研究型的語言。
3具有跨平台(Windows, Mac OSX, Linux)能力的開發框架。
微軟開發工具
開發環境
  • Code
  • Express
  • Lab Management英语Visual Studio Lab Management
  • Team System Profiler英语VSTS Profiler
  • Test Professional
  • Tools for Applications英语Visual Studio Tools for Applications
  • Tools for Office英语Visual Studio Tools for Office
其他
语言
主要API和框架
(完整列表)
数据库
管理
  • Management Studio英语SQL Server Management Studio
  • Business Intelligence Development Studio英语Business Intelligence Development Studio
SQL服务
  • Analysis英语Microsoft Analysis Services
  • Reporting
  • Integration英语SQL Server Integration Services
  • Notification英语SQL Server Notification Services
其他
源代码控制
交付

范型
过程式
面向对象
多范型
函数式
逻辑式
脚本语言
动态语言
Shell语言
ECMA-262
特定平台
.NET
JVM
特定领域
科学计算
GPU计算
  • Cg/HLSL
  • GLSL
  • Brook英语BrookGPU
  • OpenCL
  • CUDA/PTX英语Parallel Thread Execution
  • Futhark
  • SPIR-V英语Standard Portable Intermediate Representation
数据查询
可视化
传媒设计
 
其它
  • 分类 目录
  • 列表级条目 列表
  • 历史
  • 比较英语Comparison of programming languages
  • 时间线英语Timeline of programming languages
UnixWindows互操作性
类Unix子系统
开发环境和shells
Windows上的类Unix系统实用程序移植
  • GnuWin32
  • UnxUtils
Windows上的*nix运行时环境
  • Wabi英语Wabi (software)
  • Willows Toolkit for UNIX
  • Wine (WINE@Etersoft, Cedega英语Cedega, CrossOver, Darwine, Proton)
平台虚拟化软件英语Comparison of platform virtualization software
相关条目
  • APIW
  • Captive NTFS
  • NDISwrapper
  • Windows Interface Source Environment