fbpx
维基百科

PowerShell

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

PowerShell
Windows Terminal内运行的PowerShell 7
编程范型命令式管道面向对象函数式反射式
設計者杰弗里·史诺威英语Jeffrey Snover(Jeffrey Snover)
布鲁斯·帕耶特(Bruce Payette)
詹姆斯·特鲁赫(James Truher) (et al.)
實作者Microsoft
发行时间2006年11月14日,​16年前​(2006-11-14
目前版本
  • 7.3.5 (2023年6月27日;穩定版本)[1]
型態系統强类型类型安全类型推论动态类型
系统平台PowerShell: .NET
Windows PowerShell: .NET框架
操作系统Windows 7及其后续版本、LinuxmacOS
許可證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中,管理任务通常由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.

參考文獻

  1. ^ 1.0 1.1 v7.3.5 Release of PowerShell. 2023年6月27日 [2023年7月3日]. 
  2. ^ . [2017-06-10]. (原始内容存档于2022-05-11). 
  3. ^ Snover, Jeffrey. . Windows PowerShell Blog. Microsoft. 2008-05-25 [2017-06-10]. (原始内容存档于2019-01-08). 
  4. ^ Bright, Peter. . Ars Technica. Condé Nast. 2016-08-18 [2017-06-10]. (原始内容存档于2022-04-28). 
  5. ^ . 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. ^ . PowerShell documentations. 微软. 9 August 2015 [2019-12-18]. (原始内容存档于2016-10-31). 

擴展閱讀

外部連結

微软官网
  • 如何使用 PowerShell 文件 - PowerShell | Microsoft Docs
  • GitHub - PowerShell/PowerShell: PowerShell for every system! (页面存档备份,存于互联网档案馆
  • 易學易用的Windows PowerShell (页面存档备份,存于互联网档案馆
  • MSDN视频教程 (页面存档备份,存于互联网档案馆
其他

powershell, 此條目可参照英語維基百科相應條目来扩充, 2021年8月16日, 若您熟悉来源语言和主题, 请协助参考外语维基百科扩充条目, 请勿直接提交机械翻译, 也不要翻译不可靠, 低品质内容, 依版权协议, 译文需在编辑摘要注明来源, 或于讨论页顶部标记, href, template, translated, page, html, title, template, translated, page, translated, page, 标签, 包括windows, core, 是微軟公司开发的任务. 此條目可参照英語維基百科相應條目来扩充 2021年8月16日 若您熟悉来源语言和主题 请协助参考外语维基百科扩充条目 请勿直接提交机械翻译 也不要翻译不可靠 低品质内容 依版权协议 译文需在编辑摘要注明来源 或于讨论页顶部标记 a href Template Translated page html title Template Translated page Translated page a 标签 PowerShell 包括Windows PowerShell和PowerShell Core 是微軟公司开发的任务自动化和組態管理架構 由在 NET Framework和后来的 NET上构建的命令行界面殼層相关脚本语言组成 最初僅僅是Windows元件 后于2016年8月18日开源并提供跨平台支持 4 PowerShell在Windows Terminal内运行的PowerShell 7编程范型命令式 管道 面向对象 函数式 反射式設計者杰弗里 史诺威 英语 Jeffrey Snover Jeffrey Snover 布鲁斯 帕耶特 Bruce Payette 詹姆斯 特鲁赫 James Truher et al 實作者Microsoft发行时间2006年11月14日 16年前 2006 11 14 目前版本7 3 5 2023年6月27日 穩定版本 1 型態系統强类型 类型安全 类型推论和动态类型系统平台PowerShell NETWindows PowerShell NET框架操作系统Windows 7及其后续版本 Linux macOS許可證MIT許可證 2 但Windows组件仍然是专有的 文件扩展名 ps1 脚本文件 ps1xml XML文档 psc1 控制台文件 psd1 数据文件 psm1 脚本模块 pssc 会话配置文件 cdxml Cmdlet定义的XML文档 網站microsoft wbr com wbr powershell啟發語言Ksh Perl C CL 英语 IBM i Control Language DCL 英语 DIGITAL Command Language SQL Tcl Tk 3 Chef Puppet在PowerShell中 管理任务通常由cmdlets 发音为command lets 执行 这是执行特定操作的专用 NET类 可以将cmdlet集合至脚本 可执行文件 一般是独立应用程序 中 或通过常规 NET类 或WMI COM对象 实例化 5 6 目录 1 源起與目的 2 程式特性 3 技術基礎 4 範例 4 1 命令提示字元比较 5 參考文獻 6 擴展閱讀 7 外部連結源起與目的 编辑UNIX系統一直有著功能強大的殼程式 shell Windows PowerShell的誕生就是要提供功能相當於UNIX系統的命令列殼程式 例如 sh bash 或csh 同時也內建腳本語言以及輔助腳本程式的工具 程式特性 编辑一致性的設計讓所有工具和系統資料的使用語法 命名原則都相同 腳本語言簡單易學 而且能支援現有的腳本程式和命令列工具 內含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 取得互联网上的网页內容 尽管外部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 Clear Host被實現為預定義的PowerShell功能 3 0 3 1 Available in Windows NT4 Windows 98 Resource Kit Windows 2000 Support Tools 4 0 4 1 Available in Windows XP Professional Edition and later Also used in UNIX to send a process any signal the Terminate signal is merely the default curl and wget aliases are absent from PowerShell Core so as to not interfere with invoking similarly named native commands 參考文獻 编辑 1 0 1 1 v7 3 5 Release of PowerShell 2023年6月27日 2023年7月3日 PowerShell LICENSE 2017 06 10 原始内容存档于2022 05 11 Snover Jeffrey PowerShell and WPF WTF Windows PowerShell Blog Microsoft 2008 05 25 2017 06 10 原始内容存档于2019 01 08 Bright Peter PowerShell is Microsoft s latest open source release coming to Linux OS X Ars Technica Conde Nast 2016 08 18 2017 06 10 原始内容存档于2022 04 28 How Windows PowerShell works Microsoft Developer Network Microsoft 2007 11 27 原始内容存档于2008 09 05 Truher Jim Extend Windows PowerShell With Custom Commands MSDN Magazine Microsoft December 2007 2017 06 10 原始内容存档于2008 10 06 Test Connection PowerShell documentations 微软 9 August 2015 2019 12 18 原始内容存档于2016 10 31 擴展閱讀 编辑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视频教程 页面存档备份 存于互联网档案馆 其他GitHub上的PowerShell頁面 TechNet 維基 Windows PowerShell Survival Guide 页面存档备份 存于互联网档案馆 取自 https zh wikipedia org w index php title PowerShell amp oldid 73615423, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。