PowerShell

✍ dations ◷ 2025-06-17 00:24:39 #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

相关

  • bspan style=color:white;巴利阿里群岛/span/b巴利阿里群岛(加泰罗尼亚语:Illes Balears,.mw-parser-output .IPA{font-family:"Charis SIL","Doulos SIL","Linux Libertine","Segoe UI","Lucida Sans Unicode","Code2000",
  • 海螺疱疹病毒属海螺疱疹病毒属(学名:Aurivirus)是疱疹病毒目软体动物疱疹病毒科的一个属,为该科的两个属之一,以鲍鱼为宿主。本属仅有一种病毒,即海螺疱疹病毒1型(Haliotid herpesvirus 1),又称鲍鱼
  • 森佩尔戈特弗里德·森佩尔(德语:Gottfried Semper,1803年11月29日-1879年5月15日),德国建筑师、艺术评论家。森佩尔的代表作品是其于1838年至1841年间设计建造的德累斯顿森佩尔歌剧院。1
  • 霍夫曼征在医学中,Hoffmann征指的是以德国生理学家Paul Hoffmann(英语:Paul Hoffmann (physiologist))(1884–1962)命名的一种远侧神经响应的表现。Hoffmann征(或Tinel征)是通过机械刺激受伤
  • CanocoCanoco(canonical community ordination)是生态学方面多元数据排序分析的一个商业软件。
  • 珍珠岩珍珠岩台湾称珍珠石,是一种类似流纹岩的酸性火山喷发熔岩,由于喷发后急速冷却,形成球粒状玻璃质岩石,有弧形或圆形裂纹,尤如珍珠的结构,所以被命名为珍珠岩。珍珠岩一般为浅灰色、
  • 三色《三色》(英语:Three Colours,法语:Trois Couleurs)是波兰导演克日什托夫·基斯洛夫斯基导演的系列电影:蓝色、白色和红色是法国国旗从左到右的三种颜色。《三色》是电影史上的不
  • 雅特·布雷奇雅特·布雷奇,或译亚特·布雷基(英语:Art Blakey,全名Arthur Blakey,曾改名Abdullah Ibn Buhaina,1919年10月11日-1990年10月16日),是硬博普爵士乐的代表人物之一。他最大的特色是击
  • 卢炬甫卢炬甫(1947年8月-2017年11月3日),男,湖北大冶人,中国天体物理学家,厦门大学教授,研究领域为黑洞吸积盘理论及其高能天体物理应用,包括活动星系核、X射线双星、伽玛射线暴等。曾任致
  • 马特·尼古拉斯·比昂迪马休·“马特”·尼古拉斯·比昂迪(英语:Matthew "Matt" Nicholas Biondi,1965年10月8日-),美国男子游泳运动员。比昂迪曾在汉城奥运上夺得7块奖牌(其中包括5面金牌),追平了另一位美国游泳运动员马克·史毕兹创下的奥运纪录,这个纪录直到2004年雅典奥运才被迈克尔·弗雷德·菲尔普斯打破,菲尔普斯于该届奥运夺得8枚奖牌。比昂迪出生在加利福尼亚州的帕罗奥图,然后他在蒙拉加(Moraga)以游泳及水球选手开始运动员生涯。随着比昂迪进入青少年时期,他在游泳上惊人的能力便开始显露出来