Windows PowerShell

✍ dations ◷ 2025-02-24 04:16:32 #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

相关

  • 轻中量级轻中量级(英语:light middleweight),或称为初中量级(junior middleweight)、超沉量级(super welterweight),是搏击运动的体重级别之一。拳击运动中轻中量级的划分标准为不超过147磅(66
  • 蔬菜列表蔬菜列表,列举所有可以做菜、烹饪成为食品的蔬菜。分类:豆科
  • 流感嗜血杆菌疫苗流感嗜血杆菌疫苗(Haemophilus influenzae type B vaccine)是一种用作预防流感嗜血杆菌(HIB)感染的疫苗 。在有将这疫苗纳入接种计划的国家,严重的流感嗜血杆菌感染率减少到10%以
  • 2014年冬季奥林匹克运动会第二十二届冬季奥林匹克运动会(英语:the XXII Olympic Winter Games,法语:les XXIIes Jeux olympiques d'hiver,俄语:XXII Олимпийские зимние игры),于2014年
  • 法保 (赫舍里氏)参数所指定的目标页面不存在,建议更正成存在页面或直接建立下列一个页面(建立前请先搜寻是否有合适的存在页面可以取代):法保(17世纪?-1710年),或作发保,都英额赫舍里氏,满洲正黄旗人
  • 2厘米KwK 30坦克炮2厘米KwK 30L/55(德语:2 cm Kampfwagenkanone 30L/55)是德国二号坦克的主要武器。 曾在西班牙内战和第二次世界大战服役。它从1935年起由毛瑟和莱茵金属共同生产。KwK 30亦作为
  • 铃木雄介铃木雄介(日语:鈴木 雄介/すずき ゆうすけ ,1988年1月2日-)生于石川县能美郡辰口町,是一名日本男子田径运动员,主攻竞走项目,曾就读于顺天堂大学。他曾于2015年3月15日在能美市打破
  • 铝试剂铝试剂,玫红三羧酸的三铵盐,是一种常用于检测水溶液中铝离子的染料。除了在定性无机分析中的使用,铝试剂还在色素生产中有应用。 它能与铝,铬,铁和铍的离子形成色淀。铝试剂微溶
  • 安秉介安秉介,陕西省同州府郃阳县(今合阳县)人,清朝政治人物、同进士出身。光绪十八年(1892年),参加光绪壬辰科殿试,登进士三甲93名。同年五月,改翰林院庶吉士。光绪二十四年四月,散馆,授翰林
  • 沈国沈国,又名聃国,是西周至春秋时代位于江汉流域的诸侯国,是周文王最小的儿子季载(武王最小的同母弟)的封国。季载贵为西周成王朝三公之一的司空。聃国是侯国,位阶仅在公爵之下,其封域