PowerShell是一个开源、跨平台的脚本和开发语言,可让我们通过脚本实现任务的自动化。在这里介绍使用PowerShell之前的基础知识。

PowerShell版本

通过PowerShell内置变量PSVersionTable查看当前的PowerShell版本。

PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.1682
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1682
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

PowerShell命令类型

PowerShell命令分为cmdlet,函数,别名和外部脚本。微软提供的多数内置命令是cmdlet。执行Get-Command命令查看CommandType字段。

PS> Get-Command -Name Get-Alias

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Get-Alias                                          3.1.0.0    Microsoft.PowerShell.Utility

PowerShell命令通常以 动词-名词 的形式,例如已动词Get,Set,Update和Remove开头,后端面连着一个名词。
Get-Command命令指定Verb选项会获取已动词Get开始的命令。

PS> Get-Command -Verb Get


CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           Get-AppPackage                                     2.0.1.0    Appx
Function        Get-AppBackgroundTask                              1.0.0.0    AppBackgroundTask
Cmdlet          Get-LocalUser                                      1.0.0.0    Microsoft.PowerShell.LocalAccounts
~省略~

Get-Command命令指定Noun选项,完整的指定动词和名词的部分。

PS> Get-Command -Verb Get -Noun Content

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Get-Content                                        3.1.0.0    Microsoft.PowerShell.Management

PowerShell帮助

我们在Linux上使用man命令获取帮助,而Powershell提供了Get-Help命令。
如果想查看某个cmdlet的使用方法,可以将该cmdlet传给Get-Help命令获取帮助。

PS> Get-Help Add-Content -Examples

Get-Help Add-Content -Examples

NAME
    Add-Content

SYNOPSIS
    Adds content to the specified items, such as adding words to a file.


    - Example 1: Add a string to all text files with an exception -

    Add-Content -Path .\*.txt -Exclude help* -Value 'End of file'

    The Path parameter specifies all `.txt` files in the current directory, but the Exclude parameter ignores file
    names that match the specified pattern. The Value parameter specifies the text string that is written to the files.

    Use Get-Content (Get-Content.md)to display the contents of these files.
    --- Example 2: Add a date to the end of the specified files ---
~省略~

如果出现了以下信息,执行 Update-Help 命令进行更新。

PS> Get-Help Add-Content -Examples

NAME
    Add-Content

ALIASES
    ac


REMARKS
    Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
        -- To download and install Help files for the module that includes this cmdlet, use Update-Help.
        -- To view the Help topic for this cmdlet online, type: "Get-Help Add-Content -Online" or
           go to https://go.microsoft.com/fwlink/?LinkID=113278.

PowerShell执行策略

当我们编写PowerShell脚本后,运行只需在控制台中输入脚本的路径即可。但是默认情况下会遇到如下错误。

这是因为默认情况下,PowerShell的执行策略不允许运行任何脚本。执行策略是一项安全措施,用于决定哪些脚本可以运行。执行策略有以下4个等级。

执行策略 内容
Restriceted(受限) 不允许运行脚本(默认等级)
AllSigned(全面签名) 只允许运行由受信的一方加密签名的脚本
RemoteSigned(远程签名) 允许自己编写的脚本以及从别处下载的脚本,有受信的一方加密签名即可
Unrestriceted(不受限) 允许运行任何脚本

Get-ExecutionPolicy 命令确认当前的执行策略。

执行Get-ExecutionPolicy命令的结果通常是Restricted,已管理员身份执行Set-ExecutionPolicy命令将执行策略修改为RemoteSigned。

关于PowerShell的签名参考下面的文章。

PowerShell Basics – Execution Policy and Code Signing

PowerShell中的对象

PowerShell数据类型有布尔值,整数,浮点数,字符串等,但一切皆为对象。查看对象的属性和Method,定义变量 $color 后,使用Select-Object及Get-Member 命令查看属性和Method。

$color = “Red” 定义变量color后代入”Red”字符。

Select-Object -InputObject $color -Property *命令查看,字符串$color的属性,在这里可确认到字符串$color有Length属性。

Get-Member -InputObject $color命令查看,字符串提供的Method(方法)。