Windows PowerShell

✍ dations ◷ 2025-12-05 05:24:36 #Windows PowerShell

PowerShell(包括Windows PowerShell和PowerShell Core)是微软公司开发的任务自动化和配置管理框架,由.NET Framework和.NET Core是构建的命令行界面壳层相关脚本语言组成,最初仅仅是Windows组件,后于2016年8月18日开源并跨平台支持。

在PowerShell中,管理任务通常由cmdlets(发音为command-lets)执行,这是执行特定操作的专用.NET类。可以将cmdlet集合至脚本、可执行文件(一般是独立应用程序)中,或通过常规.NET类(或WMI / COM对象)实例化。通过访问不同数据存储中的数据由PowerShell运行,如资源管理器或注册表。

UNIX系统一直有着功能强大的壳程序(shell),Windows PowerShell的诞生就是要提供功能相当于UNIX系统的命令行壳程序(例如:sh、bash或csh),同时也内置脚本语言以及辅助脚本程序的工具。

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。

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 = (new-object System.Net.WebClient).DownloadString($rssUrl)$blog.rss.channel.item | select title -first 8

与命令提示符等的比较

PowerShell命令行与其他命令行解释器的内部和外部命令的比较
PowerShell(命令行)PowerShell(别名)命令提示符Unix shell描述
Get-ChildItemgci, dir, lsdirls列出目前或指定文件夹中的所有文件和文件夹
Test-Connectionpingpingping从目前电脑向指定电脑发送Ping,或指示另一台电脑这样做
Get-Contentgc, type, cattype(英语:TYPE (DOS command))cat获取文件内容
Get-Commandgcmhelp(英语:help (command))type(英语:type (Unix)), which(英语:which (command)), compgen列出可用的命令
Get-Helphelp, manhelp(英语:help (command))apropos(英语:apropos (Unix)), man在控制台上打印命令的文档
Clear-Hostcls, clearcls(英语:cls (computing))clear清除屏幕
Copy-Itemcpi, copy, cpcopy, xcopy(英语:xcopy), robocopy(英语:robocopy)cp将文件和文件夹复制到另一个位置
Move-Itemmi, move, mvmove(英语:move (command))mv将文件和文件夹移动到新位置
Remove-Itemri, del, erase, rmdir, rd, rmdel(英语:del (command)), erase(英语:del (command)), rmdir, rdrm, rmdir删除文件或文件夹
Rename-Itemrni, ren, mvren(英语:ren (command)), renamemv重命名单个文件、文件夹、硬链接或符号链接
Get-Locationgl, cd, pwdcdpwd显示工作路径(目前文件夹)
Pop-Locationpopdpopd(英语:pushd and popd)popd将工作路径更改为最近推送到堆栈上的位置
Push-Locationpushdpushd(英语:pushd and popd)pushd将工作路径存储到堆栈中
Set-Locationsl, cd, chdircd, chdircd改变工作路径
Tee-Objecttee不适用tee将输入管道传输到文件或变量,并沿管道传递输入
Write-Outputecho, writeechoecho将字符串或其他对像打印到标准流
Get-Processgps, pstlist, tasklist(英语:tasklist)ps列出所有正在运行的进程
Stop-Processspps, killkill(英语:kill (command)), taskkillkill停止正在运行的进程
Select-Stringslsfindstrfind, grep打印与模式匹配的行
Set-Variablesv, setset(英语:Environment variable#DOS)env, export, set, setenv创建或更改环境变量的内容
Invoke-WebRequestiwr, curl, wgetcurlwget, 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.
  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. ^ Release v7.1.3 Release of PowerShell. GitHub. . 
  2. ^ PowerShell LICENSE
  3. ^ Snover, Jeffrey. PowerShell and WPF: WTF. Windows PowerShell Blog. Microsoft. 2008-05-25. 
  4. ^ Bright, Peter. PowerShell is Microsoft's latest open source release, coming to Linux, OS X. Ars Technica. Condé Nast. 2016-08-18. 
  5. ^ How Windows PowerShell works. Microsoft Developer Network. Microsoft. . 
  6. ^ Truher, Jim. Extend Windows PowerShell With Custom Commands. MSDN Magazine (Microsoft). December 2007 . (原始内容存档于2008-10-06). 
  7. ^ Test-Connection. PowerShell documentations. 微软. 9 August 2015. 

扩展阅读

  • 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

相关

  • Pioglitazone吡格列酮(Pioglitazone)是一种专门用于治疗2型糖尿病的口服药物,属于噻唑烷二酮类(活化受体调节剂)药物的一种。
  • CD2211IGR、​1JQH、​1K3A、​1M7N、​1P4O、​2OJ9、​2ZM3、​3D94、​3F5P、​3I81、​3LVP、​3LW0、​3NW5、​3NW6、​3NW7、​3O23、​3QQU、​4D2R、​4XSS、​5HZN3480
  • 可敬者在基督教信仰中,可敬者(英语:The Venerable),又译为尊者,是一个特别的头衔。在罗马天主教会中,它是封圣的过程之一,用来表扬在教会中,具备英雄般美德的信仰者。可敬者是神仆(Servant o
  • 脱氧鸟苷单磷酸去氧鸟苷单磷酸(Deoxyguanosine monophosphate,dGMP)是一种结构与鸟苷单磷酸相似,但五碳糖的2号碳上少了一个-OH基的分子,并由单一的氢原子取而代之。
  • 姜杰 (科学家)姜杰(1961年-),汉族,女,黑龙江哈尔滨人,毕业于国防科学技术大学,中华人民共和国科学家、第十一届全国政协委员。2015年12月当选中国科学院院士。担任中国航天科技集团公司科技委常委
  • 丹尼尔·布尔斯廷丹尼尔·布尔斯廷(,1914年10月1日-2004年2月28日),美国历史学家、博物学家和前美国国会图书馆馆长。1914年出生在美国南部的亚特兰大,2004年2月28日因肺炎在华盛顿去世。他一生中
  • 榊原键吉榊原 键吉(さかきばら けんきち,文政13年11月5日(1830年12月19日) - 明治27年(1894年)9月11日)为江户幕府幕臣,剑术家。讳友善(ともよし)。幕末时,从男谷信友手中继承直心影流‧男谷派
  • 湖积平原湖积平原(英语:Lacustrine Plains)是原本为湖泊的一部分,但由于自然排水、蒸发等原因,湖水浸没范围缩小,水下堆积面露出而形成的平原。
  • 柯柯站柯柯站,位于中华人民共和国青海省海西蒙古族藏族自治州乌兰县柯柯镇,是青藏铁路上的火车站,建于1979年,由青藏铁路公司管辖。
  • HimekaHimeka(ヒメカ)(1981年7月20日-),加拿大魁北克出生的法裔女性歌手。本名Catherine St-Onge,母语为法语。热爱动漫(动画、漫画、游戏等等),并认为动漫能够带给人们元气、希望与勇气。目