PowerShell

✍ dations ◷ 2025-11-21 18:06:37 #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.4 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

相关

  • 久效磷久效磷(英语:Monocrotophos)是一种有机磷杀虫剂,对人和鸟类也具有毒性。作为一种持久性有机污染物,已在美国等一些国家禁止使用。久效磷主要用于农业,且是一个相对便宜的农药。然
  • 查理 (瓦卢瓦伯爵)瓦卢瓦的查理(法语:Charles de Valois,1270年3月12日-1325年12月16日)是法国的瓦卢瓦伯爵和安茹伯爵。他是统治了法国近300年的瓦卢瓦王朝的始祖。瓦卢瓦的查理是法国国王腓力三
  • 阪急百货店阪急百货店是日本一间百货公司。以关西为基地,总店位于大阪市。为阪急阪神百货店的子公司,属于阪急阪神东宝集团旗下的H2O零售集团。
  • 海伦·寇蒂卡海伦·玛丽·寇蒂卡(英语:Helen Mary Caldicott,1938年8月7日-),生于澳洲墨尔本,澳大利亚籍医师、反核运动推动者,曾建立数个反核运动团体,其中一个由她参与协助成立的国际医生防止核
  • 塔尔切尔塔尔切尔(Talcher),是印度奥里萨邦Anugul县的一个城镇。总人口34984(2001年)。该地2001年总人口34984人,其中男性19267人,女性15717人;0—6岁人口4307人,其中男2258人,女2049人;识字率7
  • 梁国治梁国治(1723年-1786年),字阶平,号瑶峰、丰山,浙江会稽中塘梁巷村(今属上虞)人。清朝政治人物、书法家。父梁文标,当过刑部司狱,有惠政。国治自幼颖悟,乾隆六年(1741年)中举,乾隆十三年(1748
  • 2013年墨西哥周末票房冠军下列列表为2013年 墨西哥周末电影票房冠军。
  • 杨九郎杨九郎(1989年7月17日-),本名杨淏翔,字“九郎”,身高cm181。毕业于北京城市学院,粉丝群体称作“栗子壳”。于2009年入科,2013年9月4日,随“九”字科第一批拜师成为“头九”成员,为德云
  • 假饵钓鱼假饵钓鱼,是一种利用金属、塑料、橡胶乃至木头制作成自然物体,引诱鱼儿上钩的钓鱼方法。鱼饵往往被做成鱼类所喜欢的食物的样子,同时钓鱼者在钓鱼时还要摆动鱼饵,使其更像是活
  • 科颜氏科颜氏(Kiehl's LLC)是美国的一家化妆品公司。在其创业之初的1851年,科颜氏是一家位于曼哈顿的药妆店。2000年,科颜氏被欧莱雅收购。现在科颜氏在全世界有超过250个店铺,并有超过