PowerShell是微软基于DotNet平台推出的一套命令行脚本语言和工具。和传统的Shell比如Bash或者Zsh不同,PowerShell是一款面向对象的Shell。简单的说,PowerShell的各个命令(Cmdlet)之间交换的是Object,而不是像传统的Shell那样交换的是文本数据。就连Cmdlet本身也是一个基于DotNet的对象,而不仅仅是一个可执行程序。
PowerShell的版本
PowerShell最早的版本1.0出现在Windows Server 2008上。Windows 7安装的是PowerShell 2.0版本,而到了Windows 10,PowerShell的版本则升到了5.0。随着DotNet的跨平台化,从6.0版本开始PowerShell变成了PowerShell Core,与之对应的是DotNet Core,这两者都可以在Windows、Mac、Linux上运行。关于更多版本信息,可以参考这个文章:Differences between PowerShell versions。
如果你的PowerShell版本过低,可以通过Chocolatey来升级:
choco install powershell
上面的命令可以把PowerShell升级到最新的、非PowerShell Core的版本。
PowerShell的特色
Cmdlet的命名规则
PowerShell即可以运行命令行命令,比如git
,也可以运行Cmdlet。Cmdlet是一个基于DotNet的对象,可以用多种语言来编写,比如PowerShell脚本、IronPython、VB、C#等等。
Cmdlet有自己的一套规范,以Get-Help
为例,这是一个动词-名词
的组合。其他的例子还有:Set-Location、Clear-Host、Get-Member等等。在PowerShell里面执行Get-Help -Category Cmdlet
,可以列出所有Cmdlet。
另外,Powershell不区分大小写,所以Get-Help
等同于get-help
。对于一个具体的命令,也可以在命令后面加-?
来查看相关的帮助。
PowerShell还支持Alias,也就是别名,可以通过Get-Alias
查看。我们可以看到,很多常用的Unix命令,在PowerShell里面都是一些Cmdlet的别名:
CommandType Name Version Source
----------- ---- ------- ------
...
Alias cat -> Get-Content
Alias cd -> Set-Location
Alias chdir -> Set-Location
Alias clear -> Clear-Host
Alias cls -> Clear-Host
Alias compare -> Compare-Object
Alias cp -> Copy-Item
...
帮助文件可升级
在高版本的PowerShell里面,可以通过Update-Help
这个Cmdlet来在线升级帮助文件。
注意:
Update-Help
需要以管理员权限运行。
升级过后执行Get-Help -cat HelpFile
,可以看到许多以about_
开头的帮助文件:
Name Category
---- --------
about_ActivityCommonParameters HelpFile
about_Aliases HelpFile
about_Arithmetic_Operators HelpFile
about_Arrays HelpFile
这些个帮助文件可以告诉你PowerShell自身的脚本语法,以及其他一些内容。比如,help about_foreach
告诉你怎么在PowerShell里面使用foreach
这个循环控制结构。找起来挺方便的,比Bash、Zsh的帮助要简单亲民一些。
在线的PowerShell文档也很齐全。有一个PowerShell Module Browser可以方便帮你找到PowerShell的Module信息。
PowerShellGet包管理
PowerShell支持通过Module来扩展功能。有一个在线的网站https://www.powershellgallery.com/有好多现成的Module可以下载。PowerShell 5.0往上版本自带了一个叫PowerShellGet的Module可以帮你自动从powershellgallery.com下载和安装Module。比如下面这个例子自动安装PSReadline Module:
Install-Module -Name PSReadLine
上面的命令需要管理员权限,因为PSReadLine会安装到系统目录,所有用户都可以用。如果为当前用户安装的话,可以使用下面的命令:
Install-Module -Name PsReadline -Scope CurrentUser
PsRealine从1.2版本开始支持Vi模式的命令行编辑,可以通过以下命令启用:
Set-PSReadlineOption -EditMode vi
可以把这一样加到你的$Profile
里面(通常是C:\Users\name\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
),如果不存在的话,可以使用以下命令创建:
new-item -type file $PROFILE
不过$PROFILE
默认不会加载,会出现以下错误:
. : File C:\Users\name\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 cannot be loaded because
running scripts is disabled on this system. For more information, see about_Execution_Policies at
http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:3
+ . 'C:\Users\name\Documents\WindowsPowerShell\Microsoft.PowerShell_ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
这是因为PowerShell默认的ExecutionPolicy为Restricted,会禁止从本地加载脚本。在有管理员权限的PowerShell里面执行以下命令来把权限稍微降低一些:
Set-ExecutionPolicy RemoteSigned
关于权限,更多请参考PowerShell says “execution of scripts is disabled on this system.”
参考
- StackOverflow: Which .NET version is my PowerShell script using?
- PowerShell script to return versions of .NET Framework on a machine?
- PowerShell Syntax - PowerShell - SS64.com
- 5 Cmdlets to Get You Started with PowerShell
- PowerShell Commands Every Developer Should Know: 50+ Cmdlets for Getting Things Done, Monitoring Performance, Debugging
- Windows Powershell Vim Keybindings
- Windows PowerShell: Scripting Crash Course不错的英文简介
- PowerShell Beginner’s Guide这个英文文档是不错的入门资料。
- PowerShell One-Liners: Variables, Parameters, Properties, and Objects不错的Cheetsheet
- Weekend Scripter: PowerShell and Chocolatey