Windows PowerShell

✍ dations ◷ 2025-09-11 04:29:14 #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

相关

  • 高苑科技大学高苑科技大学,座落高雄科学园区的产业型科技大学,其前身是民国七十五年开始筹设,七十八年奉教育部令正式招生的“私立高苑工业专科学校”。八十年更名为“私立高苑工商专科学校
  • 公务员惩戒委员会公务员惩戒委员会,通常被简称为公惩会,属中华民国司法院下设的机关,与最高法院、最高行政法院同为中华民国司法制度中的终审法院。公惩会掌理惩戒权,负责对违法失职的公务员为惩
  • 第一次牛奔河之役第一次马纳萨斯战役(First Battle of Manassas)也称第一次布尔河战役(First Battle of Bull Run)于1861年7月21日发生在弗吉尼亚的马纳萨斯和布尔河附近,是第一场南北战争中的重
  • 永州九嶷山舜帝陵景区九嶷山是位于中国湖南省永州市宁远县与蓝山县一带,属于南岭之萌渚岭。九嶷山主峰为畚箕窝,其海拔1959米。九嶷山相传为舜帝安葬之地。
  • 圆山隧道圆山隧道,位于台湾台北市中山区中山北路三段与四段间的一座公路隧道,单孔单向设计,为中山区与内湖区通往士林区的重要交通枢纽。该工程列入台北市第一期四年工务建设计划,由台北
  • 白汝珊白汝珊是中国电视公司(中视)1999年退休导播,被誉为“台湾最美丽的女导播”。她担任中视综艺节目《飞扬的音符》导播期间,以重叠画面或刻意经营的镜位摆设,使舞者或伴奏乐师在萤幕
  • 惑星大战争惑星大战争(わくせいだいせんそう)是1977年上映的日本一部科幻电影。
  • 世界智力精英运动会世界智力精英运动会(英语:SportAccord World Mind Games)由国际单项运动总会联合会提供赞助。于2011年12月9-16日在北京国际会议中心举行第二届智力精英运动会于2012年12月9-20
  • 他拉干水库他拉干水库,位于中华人民共和国内蒙古自治区开鲁县县城开鲁镇以北约25千米,是新开河流域内一座大型旁侧平原水库,由原天然的他拉干泡子改造而成。水库以引洪、灌溉为主,兼有养殖
  • 美国血液学会美国血液学会(英语:American Society of Hematology)是一个美国血液学组织,也是全球最大的血液学专业协会。1958年成立于美国华盛顿哥伦比亚特区,每年12月会举办年会,吸引3万名专