PowerShell
PowerShell(包括Windows PowerShell和PowerShell Core)是微软公司开发的任务自动化和组态管理架构,由在.NET Framework和后来的.NET上构建的命令行界面壳层相关脚本语言组成,最初仅仅是Windows组件,后于2016年8月18日开源并提供跨平台支持。[4]
![]() | |
![]() 在Windows Terminal内运行的PowerShell 7 | |
命令式、管道、面向对象、函数式、反射式 | |
设计者 | 杰弗里·史诺威(Jeffrey Snover) 布鲁斯·帕耶特(Bruce Payette) 詹姆斯·特鲁赫(James Truher) (et al.) |
实作者 | Microsoft |
2006年11月14日 | |
当前版本 |
|
型态系统 | 强类型、类型安全、类型推论和动态类型 |
系统平台 | PowerShell: .NET Windows PowerShell: .NET框架 |
操作系统 | Windows 7及其后续版本、Linux、macOS |
许可证 | MIT许可证[2](但Windows组件仍然是专有的) |
文档扩展名 |
|
网站 | |
启发语言 | |
Ksh、Perl、C#、CL、DCL、SQL、Tcl、Tk、[3]Chef、Puppet |
在PowerShell中,管理任务通常由cmdlets(发音为command-lets)执行,这是执行特定操作的专用.NET类。可以将cmdlet集合至脚本、可执行文档(一般是独立应用进程)中,或通过常规.NET类(或WMI / COM对象)实例化。[5][6]
源起与目的
UNIX系统一直有着功能强大的壳程序(shell),Windows PowerShell的诞生就是要提供功能相当于UNIX系统的命令行壳程序(例如:sh、bash或csh),同时也内置脚本语言以及辅助脚本程序的工具。
程序特性
- 一致性的设计让所有工具和系统数据的使用语法、命名原则都相同。
- 脚本语言简单易学,而且能支持现有的脚本程序和命令行工具。
- 内含129种称为cmdlet的标准工具,可用来处理常见的系统管理工作。
- 具备完整的扩充功能,独立软件商或开发者都能很容易的自行扩充。
- 进程间数据传递内容具有强类型特征。
- PowerShell Core可以自动检测字符编码,自动识别Linux和Windows下的换行符,跨平台运行。
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。
范例
- 停止所有目前运行中的以"p"字符开头命名的程序:
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 = [xml](new-object System.Net.WebClient).DownloadString($rssUrl)
$blog.rss.channel.item | select title -first 8
命令提示字符比较
PowerShell(命令行) | PowerShell(别名) | 命令提示符 | Unix shell | 描述 |
---|---|---|---|---|
Get-ChildItem | gci, dir, ls | dir | ls | 列出目前或指定文件夹中的所有文件和文件夹 |
Test-Connection[lower-alpha 1] | ping | ping | ping | 从目前电脑向指定电脑发送Ping,或指示另一台电脑这样做 |
Get-Content | gc, type, cat | type | cat | 取得文件内容 |
Get-Command | gcm | help | type, which, compgen | 列出可用的命令 |
Get-Help | help, man | help | apropos, man | 在控制台上打印命令的文档 |
Clear-Host | cls, clear | cls | clear | 清除屏幕[lower-alpha 2] |
Copy-Item | cpi, copy, cp | copy, xcopy, robocopy | cp | 将文件和文件夹拷贝到另一个位置 |
Move-Item | mi, move, mv | move | mv | 将文件和文件夹移动到新位置 |
Remove-Item | ri, del, erase, rmdir, rd, rm | del, erase, rmdir, rd | rm, rmdir | 删除文件或文件夹 |
Rename-Item | rni, ren, mv | ren, rename | mv | 重命名单个文件、文件夹、硬链接或符号链接 |
Get-Location | gl, cd, pwd | cd | pwd | 显示工作路径(目前文件夹) |
Pop-Location | popd | popd | popd | 将工作路径更改为最近推送到堆栈上的位置 |
Push-Location | pushd | pushd | pushd | 将工作路径保存到堆栈中 |
Set-Location | sl, cd, chdir | cd, chdir | cd | 改变工作路径 |
Tee-Object | tee | 不适用 | tee | 将输入管道传输到文件或变量,并沿管道传递输入 |
Write-Output | echo, write | echo | echo | 将字符串或其他对像打印到标准串流 |
Get-Process | gps, ps | tlist,[lower-alpha 3] tasklist[lower-alpha 4] | ps | 列出所有正在运行的处理进程 |
Stop-Process | spps, kill | kill,[lower-alpha 3] taskkill[lower-alpha 4] | kill[lower-alpha 5] | 停止正在运行的处理进程 |
Select-String | sls | findstr | find, grep | 打印与模式匹配的 |
Set-Variable | sv, set | set | env, export, set, setenv | 创建或更改环境变量的内容 |
Invoke-WebRequest | iwr, |
curl | wget, curl | 取得互联网上的网页内容 |
- 尽管外部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.[7] - Clear-Host被实现为预定义的PowerShell功能。
- Available in Windows NT4, Windows 98 Resource Kit, Windows 2000 Support Tools
- Available in Windows XP Professional Edition and later
- Also used in UNIX to send a process any signal, the "Terminate" signal is merely the default
-
curl
andwget
aliases are absent from PowerShell Core, so as to not interfere with invoking similarly named native commands.
Windows PowerShell 3.0
PowerShell 3.0 与 Windows 8 和 Windows Server 2012 集成。微软还为 Windows 7 的 Service Pack 1、Windows Server 2008 的 Service Pack 1 和 Windows Server 2008 R2 的 Service Pack 1 提供了 PowerShell 3.0。[8][9]
PowerShell 3.0 是一个更大的软件包 Windows Management Framework 3.0 (WMF3) (WMF3) 的一部分。 PowerShell 3.0 是一个更大的软件包 Windows 管理框架 3.0 (WMF3) 的一部分,其中还包含支持远程协作的 WinRM 服务。[10][11]
PowerShell 3.0 的新功能包括:[12][13]
- 计划任务:可使用 Windows 任务调度进程基础架构计划任务在预定的时间和日期运行。
- 会话重新连接:会话可以断开并重新连接。远程会话现在能更好地应对临时网络故障。
- 改进了代码编写:增加了代码自动完成(IntelliSense)和代码片段。PowerShell ISE 允许用户使用对话框填写 PowerShell cmdlet 参数。
- 授权支持:可将管理任务授权给无权执行此类任务的用户,而无需授予他们永久性的额外权限。
- 更新帮助:可使用 Update-Help 命令更新帮助文档。
参考文献
- . 2024年1月11日 [2024年1月15日].
- . [2017-06-10]. (原始内容存档于2022-05-11).
- Snover, Jeffrey. . Windows PowerShell Blog. Microsoft. 2008-05-25 [2017-06-10]. (原始内容存档于2019-01-08).
- Bright, Peter. . Ars Technica. Condé Nast. 2016-08-18 [2017-06-10]. (原始内容存档于2022-04-28).
- . Microsoft Developer Network. Microsoft. [2007-11-27]. (原始内容存档于2008-09-05).
- Truher, Jim. . MSDN Magazine (Microsoft). December 2007 [2017-06-10]. (原始内容存档于2008-10-06).
- . PowerShell documentations. 微软. 9 August 2015 [2019-12-18]. (原始内容存档于2016-10-31).
- . tfl09.blogspot.com. [2023-09-09]. (原始内容存档于2023-09-05).
- . www.microsoft.com. [2023-09-09]. (原始内容存档于2023-06-19).
- . poshnit.wordpress.com. [2023-09-09]. (原始内容存档于2023-09-05).
- . www.ntcompatible.com. [2023-09-09]. (原始内容存档于2023-09-05).
- . www.easeus.com. [2023-09-09]. (原始内容存档于2023-09-05).
- . books.google.com. [2023-09-09]. (原始内容存档于2023-09-05).
扩展阅读
- Oakley, Andy. . O'Reilly Media. 2005. ISBN 0-596-10009-4.
- Holmes, Lee. . O'Reilly Media. 2006. ISBN 0-596-52813-2.
- Holmes, Lee. . O'Reilly Media. 2007. ISBN 0-596-52849-3.
- Watt, Andrew. . Wrox Press. 2007. ISBN 0-471-94693-1.
- Kumaravel, Arul; White, Jon; Naixin Li, Michael; Happell, Scott; Xie, Guohui; Vutukuri, Krishna C. . Wrox Press. 2008. ISBN 0-470-17393-9.
- Kopczynski, Tyson; Handley, Pete; Shaw, Marco. 2nd. Pearson Education. 2009. ISBN 978-0-672-32988-3.
- Jones, Don; Hicks, Jeffery. 3rd. Sapien Technologies. 2010. ISBN 978-0-9821314-2-8.
- Finke, Douglas. . O'Reilly Media. 2012. ISBN 1-4493-2270-0.
- Wilson, Ed. . Microsoft Press. 2013. ISBN 978-0-7356-6339-8.
- Wilson, Ed. . Microsoft Press. 2014. ISBN 978-0-7356-6649-8.
外部链接
- 微软官网
- 如何使用 PowerShell 文档 - PowerShell | Microsoft Docs
- GitHub - PowerShell/PowerShell: PowerShell for every system! (页面存档备份,存于)
- 易学易用的Windows PowerShell (页面存档备份,存于)
- MSDN视频教程 (页面存档备份,存于)
- 其他