第 2 章 - 帮助系统
在一个旨在评估 PowerShell 熟练程度的实验中,首先,让两个不同的 IT 专业人员组(初学者和专家)在无法访问计算机的情况下参加笔试。 令人惊讶的是,测试分数表明两组的可比技能。 随后进行了一次测试,反映了第一个但有一个关键区别:参与者有权访问配备 PowerShell 的脱机计算机。 结果显示,这次两组之间的技能差距很大。
哪些因素促成了两项评估之间观察到的结果?
专家并不总是知道答案,但他们知道如何找出答案。
这两项测试结果中观察到的结果是因为专家不会记住数千个 PowerShell 命令。 相反,他们擅长在 PowerShell 中使用帮助系统,使它们能够发现并在必要时了解如何使用命令。
熟悉帮助系统是 PowerShell 取得成功的关键。
我听到 PowerShell 的创建者 Jeffrey Snover 多次分享类似的故事。
可发现性
PowerShell 中的编译命令称为 cmdlet,发音为 “command-let”,而不是 “CMD-let”。 cmdlet 的命名约定遵循单一 Verb-Noun 格式,使其易于发现。 例如,Get-Process
是用于确定正在运行的进程的 cmdlet,Get-Service
是检索服务列表的 cmdlet。 函数(也称为脚本 cmdlet)和别名是本文稍后讨论的其他类型的 PowerShell 命令。 术语 “PowerShell 命令” 描述 PowerShell 中的任何命令,无论它是 cmdlet、函数还是别名。
您还可以从 PowerShell 运行操作系统本机命令,例如传统的命令行程序 ping.exe
和 ipconfig.exe
。
PowerShell 中的三个核心 cmdlet
Get-Help
Get-Command
Get-Member
(第3章所述)
我经常被问及:“如何弄清楚 PowerShell 中的命令是什么?”。 Get-Help
和 Get-Command
都是在 PowerShell 中发现和理解命令的宝贵资源。
Get-Help
你在 PowerShell 中首先需要了解的是如何使用帮助系统中的 Get-Help
cmdlet。
Get-Help
是一个多用途命令,可帮助你了解如何在找到命令后使用命令。
与 Get-Command
相比,还可以以更为不同和间接的方式使用 Get-Help
来查找命令。
使用 Get-Help
查找命令时,它最初会根据输入对命令名称执行通配符搜索。 如果找不到任何匹配项,它将在系统上的所有 PowerShell 帮助文章中执行全面的全文搜索。 如果这也找不到任何结果,它将返回错误。
下面介绍如何使用 Get-Help
查看 Get-Help
cmdlet 的帮助内容。
Get-Help -Name Get-Help
从 PowerShell 版本 3.0 开始,帮助内容没有随操作系统预装。 首次运行 Get-Help
时,会出现一条消息,询问是否要将 PowerShell 帮助文件下载到计算机。
如果按 Y 来回答是,则会执行 Update-Help
cmdlet,并下载帮助内容。
Do you want to run Update-Help?
The Update-Help cmdlet downloads the most current Help files for Windows
PowerShell modules, and installs them on your computer. For more information
about the Update-Help cmdlet, see
https:/go.microsoft.com/fwlink/?LinkId=210614.
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):
如果未收到此消息,请在以管理员身份运行的提升权限的 PowerShell 会话中运行 Update-Help
。
更新完成后,将显示帮助文章。
花点时间在计算机上运行该示例,查看输出,并观察帮助系统如何组织信息。
- 名称
- 概要
- SYNTAX
- 描述
- 相关链接
- 备注
查看输出时,请记住,帮助文章通常包含大量信息,默认情况下看到的不是整个帮助文章。
参数
在 PowerShell 中运行命令时,可能需要向命令提供其他信息或输入。 通过参数可以指定更改命令行为的选项和参数。 每篇帮助文章的 命令语法 段落概述了可用的参数。
Get-Help
有多个参数,可以指定这些参数来返回整个帮助文章或命令的子集。 若要查看 Get-Help
的所有可用参数,请参阅其帮助文章的 SYNTAX 部分,如以下示例所示。
...
SYNTAX
Get-Help [[-Name] <System.String>] [-Category {Alias | Cmdlet | Provider
| General | FAQ | Glossary | HelpFile | ScriptCommand | Function |
Filter | ExternalScript | All | DefaultHelp | Workflow | DscResource |
Class | Configuration}] [-Component <System.String[]>] [-Full]
[-Functionality <System.String[]>] [-Path <System.String>] [-Role
<System.String[]>] [<CommonParameters>]
Get-Help [[-Name] <System.String>] [-Category {Alias | Cmdlet | Provider
| General | FAQ | Glossary | HelpFile | ScriptCommand | Function |
Filter | ExternalScript | All | DefaultHelp | Workflow | DscResource |
Class | Configuration}] [-Component <System.String[]>] -Detailed
[-Functionality <System.String[]>] [-Path <System.String>] [-Role
<System.String[]>] [<CommonParameters>]
Get-Help [[-Name] <System.String>] [-Category {Alias | Cmdlet | Provider
| General | FAQ | Glossary | HelpFile | ScriptCommand | Function |
Filter | ExternalScript | All | DefaultHelp | Workflow | DscResource |
Class | Configuration}] [-Component <System.String[]>] -Examples
[-Functionality <System.String[]>] [-Path <System.String>] [-Role
<System.String[]>] [<CommonParameters>]
Get-Help [[-Name] <System.String>] [-Category {Alias | Cmdlet | Provider
| General | FAQ | Glossary | HelpFile | ScriptCommand | Function |
Filter | ExternalScript | All | DefaultHelp | Workflow | DscResource |
Class | Configuration}] [-Component <System.String[]>] [-Functionality
<System.String[]>] -Online [-Path <System.String>] [-Role
<System.String[]>] [<CommonParameters>]
Get-Help [[-Name] <System.String>] [-Category {Alias | Cmdlet | Provider
| General | FAQ | Glossary | HelpFile | ScriptCommand | Function |
Filter | ExternalScript | All | DefaultHelp | Workflow | DscResource |
Class | Configuration}] [-Component <System.String[]>] [-Functionality
<System.String[]>] -Parameter <System.String> [-Path <System.String>]
[-Role <System.String[]>] [<CommonParameters>]
Get-Help [[-Name] <System.String>] [-Category {Alias | Cmdlet | Provider
| General | FAQ | Glossary | HelpFile | ScriptCommand | Function |
Filter | ExternalScript | All | DefaultHelp | Workflow | DscResource |
Class | Configuration}] [-Component <System.String[]>] [-Functionality
<System.String[]>] [-Path <System.String>] [-Role <System.String[]>]
-ShowWindow [<CommonParameters>]
...
参数集
在查看 Get-Help
的语法部分时,请注意信息似乎被重复了六次。 每个块都是单个参数集,指示 Get-Help
cmdlet 具有六组不同的参数。 仔细查看可显示每个参数集至少包含一个唯一参数,使其与其他参数不同。
参数集是互斥的。 指定仅存在于一个参数集中的唯一参数后,PowerShell 会限制你使用该参数集中包含的参数。
例如,你不能一起使用 Get-Help
的 Full 和 Detailed 参数,因为它们属于不同的参数集。
以下每个参数都属于 Get-Help
cmdlet 的不同参数集。
- 完整
- 详细
- 例子
- 在线
- 参数
- ShowWindow
命令语法
如果你不熟悉 PowerShell,要理解 语法 部分中那些神秘莫测的信息(用方括号和尖括号表示)可能会让人感到困难。 但是,学习这些语法元素对于熟悉 PowerShell 至关重要。 使用 PowerShell 帮助系统的频率越多,记住所有细微差别就越容易。
查看 Get-EventLog
cmdlet 的语法。
Get-Help Get-EventLog
以下输出显示了帮助文章的相关部分。
...
SYNTAX
Get-EventLog [-LogName] <System.String> [[-InstanceId]
<System.Int64[]>] [-After <System.DateTime>] [-AsBaseObject] [-Before
<System.DateTime>] [-ComputerName <System.String[]>] [-EntryType {Error
| Information | FailureAudit | SuccessAudit | Warning}] [-Index
<System.Int32[]>] [-Message <System.String>] [-Newest <System.Int32>]
[-Source <System.String[]>] [-UserName <System.String[]>]
[<CommonParameters>]
Get-EventLog [-AsString] [-ComputerName <System.String[]>] [-List]
[<CommonParameters>]
...
语法信息包括成对的方括号([]
)。 根据它们的用法,这些方括号提供两种不同的用途。
- 括在方括号中的元素是可选的。
- 数据类型(如
<string[]>
)后面的空方括号集表示参数可以接受作为数组或集合对象传递的多个值。
位置参数
某些 cmdlet 旨在接受位置参数。 位置参数允许你提供一个值,而无需指定参数的名称。 在位置上使用参数时,必须在命令行上的正确位置指定其值。 可以在命令帮助文章的 PARAMETERS 节中找到参数的位置信息。 显式指定参数名称时,可以按任意顺序使用参数。
对于 Get-EventLog
cmdlet,第一个参数集的第一个参数是 LogName。
LogName 括在方括号中,表示它是位置参数。
Get-EventLog [-LogName] <System.String>
由于 LogName 是一个位置参数,因此可以通过名称或位置指定它。 根据参数名称后面的尖括号,LogName 的值必须是单个字符串。 缺少括住参数名称和数据类型的方括号表示,LogName 是此特定参数集中的必需参数。
该参数集中的第二个参数是 InstanceId。 参数名称和数据类型都完全括在方括号中,这表示 InstanceId 是一个可选参数。
[[-InstanceId] <System.Int64[]>]
此外,InstanceId 有自己的方括号对,指示它是类似于 LogName 参数的位置参数。 在数据类型之后,一组空的方括号意味着 InstanceId 可以接受多个值。
交换机参数
不需要值的参数称为 switch 参数。 可以轻松识别开关参数,因为参数名称后没有数据类型。 当您指定一个 switch 参数时,其值为 true
。 如果未指定 switch 参数,则其值 false
。
第二个参数集包括一个 List 参数,这是一个 switch 参数。 指定 List 参数时,它将返回本地计算机上的事件日志列表。
[-List]
语法的简化方法
除了普通英语,还有一种更加用户友好的方法可以获得与某些命令的神秘命令语法相同的信息。 PowerShell 将 Get-Help
与 Full 参数一起使用时返回完整的帮助文章,以便更轻松地理解命令的用法。
Get-Help -Name Get-Help -Full
花点时间在计算机上运行该示例,查看输出,并观察帮助系统如何组织信息。
- 名称
- 概要
- SYNTAX
- 描述
- PARAMETERS
- 输入
- 输出
- 笔记
- 例子
- 相关链接
通过使用 Get-Help
cmdlet 指定 Full 参数,输出包含多个额外的部分。 在这些部分中,PARAMETERS 通常为每个参数提供详细说明。 但是,此信息的范围因要调查的特定命令而异。
...
-Detailed <System.Management.Automation.SwitchParameter>
Adds parameter descriptions and examples to the basic help display.
This parameter is effective only when the help files are installed
on the computer. It has no effect on displays of conceptual ( About_
) help.
Required? true
Position? named
Default value False
Accept pipeline input? False
Accept wildcard characters? false
-Examples <System.Management.Automation.SwitchParameter>
Displays only the name, synopsis, and examples. This parameter is
effective only when the help files are installed on the computer. It
has no effect on displays of conceptual ( About_ ) help.
Required? true
Position? named
Default value False
Accept pipeline input? False
Accept wildcard characters? false
-Full <System.Management.Automation.SwitchParameter>
Displays the entire help article for a cmdlet. Full includes
parameter descriptions and attributes, examples, input and output
object types, and additional notes.
This parameter is effective only when the help files are installed
on the computer. It has no effect on displays of conceptual ( About_
) help.
Required? false
Position? named
Default value False
Accept pipeline input? False
Accept wildcard characters? false
...
运行上一个命令以显示 Get-Help
命令的帮助时,你可能会注意到输出滚动得太快,以致于无法读取它。
如果使用的是 PowerShell 控制台、Windows 终端或 VS Code,并且需要查看帮助文章,help
函数非常有用。 它通过管道将 Get-Help
的输出传递给 more.com
,一次显示一页帮助内容。 我建议使用 help
函数而不是 Get-Help
cmdlet,因为它提供更好的用户体验,而且输入起来更省力。
注释
ISE 不支持使用 more.com
,因此运行 help
的工作方式与 Get-Help
相同。
在计算机上的 PowerShell 中运行以下命令。
Get-Help -Name Get-Help -Full
help -Name Get-Help -Full
help Get-Help -Full
运行上述命令时,是否观察到输出中的任何变体?
在前面的示例中,第一行使用 Get-Help
cmdlet,第二行使用 help
函数,第三行在使用 help
函数时省略 Name 参数。 由于 Name 是一个位置参数,因此第三个示例利用其位置,而不是显式声明参数的名称。
区别在于,最后两个命令的输出是按页逐页显示的。 使用 help
函数时,按空格键以显示下一页的内容,或者按 Q 退出。 如果需要在 PowerShell 中以交互方式终止任何运行命令,请按 Ctrl +C。
若要快速查找有关特定参数的信息,请使用 Parameter 参数。 此方法仅返回包含特定于参数的信息的内容,而不是整个帮助文章。 这是查找有关特定参数的信息的最简单方法。
以下示例使用 help
函数与 参数 参数,从帮助文章中返回关于 Get-Help
的 名称 参数的信息。
help Get-Help -Parameter Name
帮助信息显示,Name 参数是位置参数,当按位置使用时,必须在第一个位置(即零位置)指定。
-Name <System.String>
Gets help about the specified command or concept. Enter the name of a
cmdlet, function, provider, script, or workflow, such as `Get-Member`,
a conceptual article name, such as `about_Objects`, or an alias, such
as `ls`. Wildcard characters are permitted in cmdlet and provider
names, but you can't use wildcard characters to find the names of
function help and script help articles.
To get help for a script that isn't located in a path that's listed in
the `$env:Path` environment variable, type the script's path and file
name.
If you enter the exact name of a help article, `Get-Help` displays the
article contents.
If you enter a word or word pattern that appears in several help
article titles, `Get-Help` displays a list of the matching titles.
If you enter any text that doesn't match any help article titles,
`Get-Help` displays a list of articles that include that text in their
contents.
The names of conceptual articles, such as `about_Objects`, must be
entered in English, even in non-English versions of PowerShell.
Required? false
Position? 0
Default value None
Accept pipeline input? True (ByPropertyName)
Accept wildcard characters? true
Name 参数期望一个字符串值,这个字符串值是由参数名称旁边的 <String>
数据类型识别的。
可以使用 Get-Help
指定几个其他参数来返回帮助文章的子集。 若要查看工作原理,请在计算机上运行以下命令。
Get-Help -Name Get-Command -Full
Get-Help -Name Get-Command -Detailed
Get-Help -Name Get-Command -Examples
Get-Help -Name Get-Command -Online
Get-Help -Name Get-Command -Parameter Noun
Get-Help -Name Get-Command -ShowWindow
我通常将 help <command name>
与 Full 或 Online 参数配合使用。 如果只对示例感兴趣,请使用 示例 参数。 如果只对特定参数感兴趣,请使用 Parameter 参数。
使用 ShowWindow 参数时,它会在单独的可搜索窗口中显示帮助内容。 如果有多个监视器,可以将该窗口移动到其他监视器。 但是,ShowWindow 参数有一个已知 bug,可能会阻止它显示整个帮助文章。 ShowWindow 参数还需要具有图形用户界面(GUI)的操作系统。 当你尝试在 Windows Server Core 上使用它时,它将返回错误。
如果具有 Internet 访问权限,则可以改用 Online 参数。 Online 参数将在默认 Web 浏览器中打开帮助文章。 联机内容是最新内容。 使用浏览器可以搜索帮助内容并查看其他相关帮助文章。
注释
关于 文章不支持 Online 参数。
help Get-Command -Online
使用 Get-Help 查找命令
查找包含 Get-Help
的命令时,请为 Name 参数指定用星号(*
)通配符括起来的搜索词作为值。 下面的示例按位置使用 Name 参数。
help *process*
Name Category Module Synops
---- -------- ------ ------
Enter-PSHostProcess Cmdlet Microsoft.PowerShell.Core Con...
Exit-PSHostProcess Cmdlet Microsoft.PowerShell.Core Clo...
Get-PSHostProcessInfo Cmdlet Microsoft.PowerShell.Core Get...
Debug-Process Cmdlet Microsoft.PowerShell.M... Deb...
Get-Process Cmdlet Microsoft.PowerShell.M... Get...
Start-Process Cmdlet Microsoft.PowerShell.M... Sta...
Stop-Process Cmdlet Microsoft.PowerShell.M... Sto...
Wait-Process Cmdlet Microsoft.PowerShell.M... Wai...
Invoke-LapsPolicyProcessing Cmdlet LAPS Inv...
ConvertTo-ProcessMitigationPolicy Cmdlet ProcessMitigations Con...
Get-ProcessMitigation Cmdlet ProcessMitigations Get...
Set-ProcessMitigation Cmdlet ProcessMitigations Set...
在这种情况下,您无需添加 *
通配符字符。 如果 Get-Help
找不到与所提供的值匹配的命令,它将对该值进行全文搜索。 以下示例生成的结果与在 process
的每个末尾指定 *
通配符相同。
help process
在值中指定通配符时,Get-Help
仅搜索与提供的模式匹配的命令。 它不执行全文搜索。 以下命令不返回任何结果。
help pr*cess
如果指定一个以短划线开头的值而不将其括在引号中,则 PowerShell 将生成错误,因为它将其解释为参数名称。 Get-Help
cmdlet 不存在此类参数名称。
help -process
如果尝试搜索以 -process
结尾的命令,则必须将 *
添加到值的开头。
help *-process
使用 Get-Help
搜索 PowerShell 命令时,最好是模糊而不是过于具体。
在前面搜索 process
时,结果只返回了名称中包含 process
的命令。 但是,如果搜索 processes
,它找不到与命令名称匹配的任何结果。 如前所述,当帮助找不到任何匹配项时,它会对系统上的每个帮助文章执行全面的全文搜索,并返回这些结果。 这种类型的搜索通常产生的结果多于预期,包括与你无关的信息。
help processes
Name Category Module Synops
---- -------- ------ ------
Disconnect-PSSession Cmdlet Microsoft.PowerShell.Core Dis...
Enter-PSHostProcess Cmdlet Microsoft.PowerShell.Core Con...
ForEach-Object Cmdlet Microsoft.PowerShell.Core Per...
Get-PSHostProcessInfo Cmdlet Microsoft.PowerShell.Core Get...
Get-PSSessionConfiguration Cmdlet Microsoft.PowerShell.Core Get...
New-PSSessionOption Cmdlet Microsoft.PowerShell.Core Cre...
New-PSTransportOption Cmdlet Microsoft.PowerShell.Core Cre...
Out-Host Cmdlet Microsoft.PowerShell.Core Sen...
Start-Job Cmdlet Microsoft.PowerShell.Core Sta...
Where-Object Cmdlet Microsoft.PowerShell.Core Sel...
Debug-Process Cmdlet Microsoft.PowerShell.M... Deb...
Get-Process Cmdlet Microsoft.PowerShell.M... Get...
Get-WmiObject Cmdlet Microsoft.PowerShell.M... Get...
Start-Process Cmdlet Microsoft.PowerShell.M... Sta...
Stop-Process Cmdlet Microsoft.PowerShell.M... Sto...
Wait-Process Cmdlet Microsoft.PowerShell.M... Wai...
Clear-Variable Cmdlet Microsoft.PowerShell.U... Del...
Convert-String Cmdlet Microsoft.PowerShell.U... For...
ConvertFrom-Csv Cmdlet Microsoft.PowerShell.U... Con...
ConvertFrom-Json Cmdlet Microsoft.PowerShell.U... Con...
ConvertTo-Html Cmdlet Microsoft.PowerShell.U... Con...
ConvertTo-Xml Cmdlet Microsoft.PowerShell.U... Cre...
Debug-Runspace Cmdlet Microsoft.PowerShell.U... Sta...
Export-Csv Cmdlet Microsoft.PowerShell.U... Con...
Export-FormatData Cmdlet Microsoft.PowerShell.U... Sav...
Format-List Cmdlet Microsoft.PowerShell.U... For...
Format-Table Cmdlet Microsoft.PowerShell.U... For...
Get-Unique Cmdlet Microsoft.PowerShell.U... Ret...
Group-Object Cmdlet Microsoft.PowerShell.U... Gro...
Import-Clixml Cmdlet Microsoft.PowerShell.U... Imp...
Import-Csv Cmdlet Microsoft.PowerShell.U... Cre...
Measure-Object Cmdlet Microsoft.PowerShell.U... Cal...
Out-File Cmdlet Microsoft.PowerShell.U... Sen...
Out-GridView Cmdlet Microsoft.PowerShell.U... Sen...
Select-Object Cmdlet Microsoft.PowerShell.U... Sel...
Set-Variable Cmdlet Microsoft.PowerShell.U... Set...
Sort-Object Cmdlet Microsoft.PowerShell.U... Sor...
Tee-Object Cmdlet Microsoft.PowerShell.U... Sav...
Trace-Command Cmdlet Microsoft.PowerShell.U... Con...
Write-Information Cmdlet Microsoft.PowerShell.U... Spe...
Export-BinaryMiLog Cmdlet CimCmdlets Cre...
Get-CimAssociatedInstance Cmdlet CimCmdlets Ret...
Get-CimInstance Cmdlet CimCmdlets Get...
Import-BinaryMiLog Cmdlet CimCmdlets Use...
Invoke-CimMethod Cmdlet CimCmdlets Inv...
New-CimInstance Cmdlet CimCmdlets Cre...
Remove-CimInstance Cmdlet CimCmdlets Rem...
Set-CimInstance Cmdlet CimCmdlets Mod...
Compress-Archive Function Microsoft.PowerShell.A... Cre...
Get-Counter Cmdlet Microsoft.PowerShell.D... Get...
Invoke-WSManAction Cmdlet Microsoft.WSMan.Manage... Inv...
Remove-WSManInstance Cmdlet Microsoft.WSMan.Manage... Del...
Get-WSManInstance Cmdlet Microsoft.WSMan.Manage... Dis...
New-WSManInstance Cmdlet Microsoft.WSMan.Manage... Cre...
Set-WSManInstance Cmdlet Microsoft.WSMan.Manage... Mod...
about_Arithmetic_Operators HelpFile
about_Arrays HelpFile
about_Environment_Variables HelpFile
about_Execution_Policies HelpFile
about_Functions HelpFile
about_Jobs HelpFile
about_Logging HelpFile
about_Methods HelpFile
about_Objects HelpFile
about_Pipelines HelpFile
about_Preference_Variables HelpFile
about_Remote HelpFile
about_Remote_Jobs HelpFile
about_Session_Configuration_Files HelpFile
about_Simplified_Syntax HelpFile
about_Switch HelpFile
about_Variables HelpFile
about_Variable_Provider HelpFile
about_Windows_PowerShell_5.1 HelpFile
about_WQL HelpFile
about_WS-Management_Cmdlets HelpFile
about_Foreach-Parallel HelpFile
about_Parallel HelpFile
about_Sequence HelpFile
搜索 process
时,它返回了 12 个结果。 但是,在搜索 processes
时,它生成了 78 个结果。 如果搜索仅找到一个匹配项,Get-Help
显示帮助内容,而不是列出搜索结果。
help *hotfix*
NAME
Get-HotFix
SYNOPSIS
Gets the hotfixes that are installed on local or remote computers.
SYNTAX
Get-HotFix [-ComputerName <System.String[]>] [-Credential
<System.Management.Automation.PSCredential>] [-Description
<System.String[]>] [<CommonParameters>]
Get-HotFix [[-Id] <System.String[]>] [-ComputerName <System.String[]>]
[-Credential <System.Management.Automation.PSCredential>]
[<CommonParameters>]
DESCRIPTION
> This cmdlet is only available on the Windows platform. The
`Get-HotFix` cmdlet uses the Win32_QuickFixEngineering WMI class to
list hotfixes that are installed on the local computer or specified
remote computers.
RELATED LINKS
Online Version: https://learn.microsoft.com/powershell/module/microsoft.
powershell.management/get-hotfix?view=powershell-5.1&WT.mc_id=ps-gethelp
about_Arrays
Add-Content
Get-ComputerRestorePoint
Get-Credential
Win32_QuickFixEngineering class
REMARKS
To see the examples, type: "Get-Help Get-HotFix -Examples".
For more information, type: "Get-Help Get-HotFix -Detailed".
For technical information, type: "Get-Help Get-HotFix -Full".
For online help, type: "Get-Help Get-HotFix -Online"
您还可以使用 Get-Help
查找缺少帮助文章的命令,尽管此功能并不广为人知。 more
函数是没有帮助文章的命令之一。 要确认是否可以使用 Get-Help
查找不包含帮助文章的命令,请使用 help
函数查找 more
。
help *more*
搜索仅找到一个匹配项,因此它返回了当命令没有帮助文档时会显示的基本语法信息。
NAME
more
SYNTAX
more [[-paths] <string[]>]
ALIASES
None
REMARKS
None
PowerShell 帮助系统还包含概念性的关于帮助文章。 你必须更新系统上的帮助内容才能获取关于文章。 有关详细信息,请参阅本章 更新帮助 部分。
使用以下命令返回系统上所有 关于 帮助文章的列表。
help About_*
将结果限制为一个 关于 帮助文章时,Get-Help
显示该文章的内容。
help about_Updatable_Help
更新帮助
在本章的前面部分,你在首次运行 Get-Help
cmdlet 时更新了计算机上的 PowerShell 帮助文章。 应定期在计算机上运行 Update-Help
cmdlet,以获取对帮助内容的任何更新。
重要
在 Windows PowerShell 5.1 中,必须在提升的 PowerShell 会话中以管理员身份运行 Update-Help
。
在以下示例中,Update-Help
下载计算机上安装的所有模块的 PowerShell 帮助内容。 应使用 Force 参数来确保下载最新版本的帮助内容。
Update-Help -Force
如以下结果所示,模块返回错误。 错误并不常见,通常在模块的作者未正确配置可更新帮助时发生。
Update-Help : Failed to update Help for the module(s) 'BitsTransfer' with UI
culture(s) {en-US} : Unable to retrieve the HelpInfo XML file for UI culture
en-US. Make sure the HelpInfoUri property in the module manifest is valid or
check your network connection and then try the command again.
At line:1 char:1
+ Update-Help
+ ~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Update-Help], Except
ion
+ FullyQualifiedErrorId : UnableToRetrieveHelpInfoXml,Microsoft.PowerShe
ll.Commands.UpdateHelpCommand
Update-Help
需要 Internet 访问权限才能下载帮助内容。 如果计算机没有 Internet 访问权限,请在具有 Internet 访问权限的计算机上使用 Save-Help
cmdlet 下载并保存更新的帮助内容。 然后,使用 Update-Help
的 SourcePath 参数指定已保存的已更新帮助内容的位置。
Get-Command
Get-Command
是另一个多用途命令,可帮助你查找命令。 在没有任何参数的情况下运行 Get-Command
时,它会返回系统上所有 PowerShell 命令的列表。
还可以使用 Get-Command
获取类似于 Get-Help
的命令语法。
如何确定 Get-Command
的语法? 可以使用 Get-Help
显示 Get-Command
的帮助文章,如本章 Get-Help 部分所示。 还可以将 Get-Command
与 语法 参数一起使用,以查看任何命令的语法。 此快捷方式可帮助你快速确定如何在不浏览其帮助内容的情况下使用命令。
Get-Command -Name Get-Command -Syntax
将 Get-Command
与 语法 参数结合使用可提供更简洁的语法视图,用于显示参数及其值类型,而不列出特定允许的值,如 Get-Help
显示。
Get-Command [[-ArgumentList] <Object[]>] [-Verb <string[]>]
[-Noun <string[]>] [-Module <string[]>]
[-FullyQualifiedModule <ModuleSpecification[]>] [-TotalCount <int>]
[-Syntax] [-ShowCommandInfo] [-All] [-ListImported]
[-ParameterName <string[]>] [-ParameterType <PSTypeName[]>]
[<CommonParameters>]
Get-Command [[-Name] <string[]>] [[-ArgumentList] <Object[]>]
[-Module <string[]>] [-FullyQualifiedModule <ModuleSpecification[]>]
[-CommandType <CommandTypes>] [-TotalCount <int>] [-Syntax]
[-ShowCommandInfo] [-All] [-ListImported] [-ParameterName <string[]>]
[-ParameterType <PSTypeName[]>] [<CommonParameters>]
如果需要有关如何使用命令的更多详细信息,请使用 Get-Help
。
help Get-Command -Full
Get-Help
的 SYNTAX 部分通过扩展参数的枚举值来提供更用户友好的显示。 它显示可以使用的实际值,以便更轻松地了解可用选项。
...
Get-Command [[-Name] <System.String[]>] [[-ArgumentList]
<System.Object[]>] [-All] [-CommandType {Alias | Function | Filter |
Cmdlet | ExternalScript | Application | Script | Workflow |
Configuration | All}] [-FullyQualifiedModule
<Microsoft.PowerShell.Commands.ModuleSpecification[]>] [-ListImported]
[-Module <System.String[]>] [-ParameterName <System.String[]>]
[-ParameterType <System.Management.Automation.PSTypeName[]>]
[-ShowCommandInfo] [-Syntax] [-TotalCount <System.Int32>]
[<CommonParameters>]
Get-Command [[-ArgumentList] <System.Object[]>] [-All]
[-FullyQualifiedModule
<Microsoft.PowerShell.Commands.ModuleSpecification[]>] [-ListImported]
[-Module <System.String[]>] [-Noun <System.String[]>] [-ParameterName
<System.String[]>] [-ParameterType
<System.Management.Automation.PSTypeName[]>] [-ShowCommandInfo]
[-Syntax] [-TotalCount <System.Int32>] [-Verb <System.String[]>]
[<CommonParameters>]
...
Get-Command
帮助的 PARAMETERS 部分显示 Name、Noun 和 Verb 参数接受通配符。
...
-Name <System.String[]>
Specifies an array of names. This cmdlet gets only commands that
have the specified name. Enter a name or name pattern. Wildcard
characters are permitted.
To get commands that have the same name, use the All parameter. When
two commands have the same name, by default, `Get-Command` gets the
command that runs when you type the command name.
Required? false
Position? 0
Default value None
Accept pipeline input? True (ByPropertyName, ByValue)
Accept wildcard characters? true
-Noun <System.String[]>
Specifies an array of command nouns. This cmdlet gets commands,
which include cmdlets, functions, and aliases, that have names that
include the specified noun. Enter one or more nouns or noun
patterns. Wildcard characters are permitted.
Required? false
Position? named
Default value None
Accept pipeline input? True (ByPropertyName)
Accept wildcard characters? true
-Verb <System.String[]>
Specifies an array of command verbs. This cmdlet gets commands,
which include cmdlets, functions, and aliases, that have names that
include the specified verb. Enter one or more verbs or verb
patterns. Wildcard characters are permitted.
Required? false
Position? named
Default value None
Accept pipeline input? True (ByPropertyName)
Accept wildcard characters? true
...
以下示例将 *
通配符与 Get-Command
的 Name 参数值配合使用。
Get-Command -Name *service*
将通配符与 Get-Command
的 Name 参数一起使用时,它会返回 PowerShell 命令和本机命令,如以下结果中所示。
CommandType Name Version
----------- ---- -------
Function Get-NetFirewallServiceFilter 2.0.0.0
Function Set-NetFirewallServiceFilter 2.0.0.0
Cmdlet Get-Service 3.1.0.0
Cmdlet New-Service 3.1.0.0
Cmdlet New-WebServiceProxy 3.1.0.0
Cmdlet Restart-Service 3.1.0.0
Cmdlet Resume-Service 3.1.0.0
Cmdlet Set-Service 3.1.0.0
Cmdlet Start-Service 3.1.0.0
Cmdlet Stop-Service 3.1.0.0
Cmdlet Suspend-Service 3.1.0.0
Application SecurityHealthService.exe 10.0.2...
Application SensorDataService.exe 10.0.2...
Application services.exe 10.0.2...
Application services.msc 0.0.0.0
Application TieringEngineService.exe 10.0.2...
Application Windows.WARP.JITService.exe 10.0.2...
可以使用 CommandType 参数将 Get-Command
的结果限制为 PowerShell 命令。
Get-Command -Name *service* -CommandType Cmdlet, Function, Alias, Script
另一个选项可能是使用 谓词 或 名词 参数,或同时使用,因为只有 PowerShell 命令具有谓词和名词。
以下示例使用 Get-Command
查找计算机上处理进程的命令。
使用 名词 参数并指定 Process
作为其值。
Get-Command -Noun Process
CommandType Name Version
----------- ---- -------
Cmdlet Debug-Process 3.1.0.0
Cmdlet Get-Process 3.1.0.0
Cmdlet Start-Process 3.1.0.0
Cmdlet Stop-Process 3.1.0.0
Cmdlet Wait-Process 3.1.0.0
摘要
本章介绍了如何使用 Get-Help
和 Get-Command
查找命令。 你还学会了如何使用帮助系统来理解在找到命令后怎样使用它们。 此外,你还了解了在新的帮助内容可用时如何更新计算机上的帮助系统。
回顾
Get-Service
的 DisplayName 参数是否是位置参数?Get-Process
cmdlet 有多少个参数集?- 使用事件日志时,存在哪些 PowerShell 命令?
- 返回计算机上运行的 PowerShell 进程的列表的 PowerShell 命令是什么?
- 如何更新存储在计算机上的 PowerShell 帮助内容?
参考
若要详细了解本章中介绍的概念,请阅读以下 PowerShell 帮助文章。
后续步骤
在下一章中,你将了解对象、属性、方法和 Get-Member
cmdlet。