PowerShell

维基百科,自由的百科全书
跳转到导航 跳转到搜索
PowerShell
File:PowerShell Core 6.0 icon.png
File:PowerShel 7.5.5 with Windows Terminal.png
Windows Terminal内运行的PowerShell 7
编程范型命令式管道面向对象函数式反射式
設計者杰弗里·史诺威英语Jeffrey Snover(Jeffrey Snover)
布鲁斯·帕耶特(Bruce Payette)
詹姆斯·特鲁赫(James Truher) (et al.)
實作者Microsoft
发行时间2006年11月14日,​19年前​(2006-11-14
当前版本
    Module:EditAtWikidata第29行Lua错误:attempt to index field 'wikibase' (a nil value)
    型態系統强类型类型安全类型推论动态类型
    系统平台PowerShell: .NET
    Windows PowerShell: .NET框架
    操作系统Windows 7及其后续版本、LinuxmacOS
    許可證MIT許可證[1](但Windows组件仍然是专有的
    文件扩展名
    • .ps1 (脚本文件)
    • .ps1xml(XML文档)
    • .psc1(控制台文件)
    • .psd1(数据文件)
    • .psm1(脚本模块)
    • .pssc(会话配置文件)
    • .cdxml (Cmdlet定义的XML文档)
    網站{{URL|example.com|可选的显示文本}}Module:EditAtWikidata第29行Lua错误:attempt to index field 'wikibase' (a nil value)
    受影响于
    KshPerlC#CL英语IBM i Control LanguageDCL英语DIGITAL Command LanguageSQLTclTk[2]ChefPuppet

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

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

    源起與目的[编辑]

    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英语compgen 列出可用的命令
    Get-Help help, man help英语help (command) apropos英语apropos (Unix), man 在控制台上打印命令的文档
    Clear-Host cls, clear cls clear 清除屏幕[b]
    Copy-Item cpi, copy, cp copy, xcopy英语xcopy, 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 popd 將工作路徑更改為最近推送到堆疊上的位置
    Push-Location pushd pushd 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,[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.[6]
    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。[7][8]

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

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

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

    安全性[编辑]

    PowerShell 5.1存在默认行为缺陷(CVE-2025-54100),攻击者通过社交工程诱导用户执行带污染参数的命令或恶意HTTP,可进行命令注入,该漏洞已在2025年12月修复[13][14]

    參考文獻[编辑]

    1. ^ PowerShell LICENSE. [2017-06-10]. (原始内容存档于2022-05-11). 
    2. ^ Snover, Jeffrey. PowerShell and WPF: WTF. Windows PowerShell Blog. Microsoft. 2008-05-25 [2017-06-10]. (原始内容存档于2019-01-08). 
    3. ^ 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). 
    4. ^ How Windows PowerShell works. Microsoft Developer Network. Microsoft. [2007-11-27]. (原始内容存档于2008-09-05). 
    5. ^ Truher, Jim. Extend Windows PowerShell With Custom Commands. MSDN Magazine (Microsoft). December 2007 [2017-06-10]. (原始内容存档于2008-10-06). 
    6. ^ Test-Connection. PowerShell documentations. 微软. 9 August 2015 [2019-12-18]. (原始内容存档于2016-10-31). 
    7. ^ PowerShell Version 3 is RTM!. tfl09.blogspot.com. [2023-09-09]. (原始内容存档于2023-09-05). 
    8. ^ WMF 3.0. www.microsoft.com. [2023-09-09]. (原始内容存档于2023-06-19). 
    9. ^ Powershell Version. poshnit.wordpress.com. [2023-09-09]. (原始内容存档于2023-09-05). 
    10. ^ Microsoft has released Windows Management Framework 3.0 - Community Technology Preview. www.ntcompatible.com. [2023-09-09]. (原始内容存档于2023-09-05). 
    11. ^ What Is PowerShell ISE?. www.easeus.com. [2023-09-09]. (原始内容存档于2023-09-05). 
    12. ^ Introducing Windows 8: An Overview for IT Professionals. books.google.com. [2023-09-09]. (原始内容存档于2023-09-05). 
    13. ^ PowerShell 5.1: Invoke-WebRequest: Preventing script execution from web content - Microsoft Support. support.microsoft.com. [2025-12-30]. 
    14. ^ Security Update Guide - Microsoft Security Response Center. msrc.microsoft.com. [2025-12-30]. 

    擴展閱讀[编辑]

    外部連結[编辑]

    微软官网
    其他