文章目录
- 其他参考连接
优质教程(learn by microsoft)
https://docs.microsoft.com/zh-cn/learn/paths/powershell/
[文档内置教程]:
Discover PowerShell - PowerShell | Microsoft Docs
除了系统控制操作之外,字符串部分是很核心的,毕竟powershell主要是字符界面
文档
专业的语言规范和语言参考
Windows PowerShell Language Specification 3.0 - PowerShell | Microsoft Docs
- 1. Introduction
- 2. Lexical Structure
- 3. Basic concepts
- 4. Types
- 5. Variables
- 6. Conversions
- 7. Expressions
- 8. Statements
- 9. Arrays
- 10. Hashtables
- 11. Modules
- 12. Attributes
- 13. Cmdlets
- A. Comment-Based Help
- B. Grammar
- [C. References]
reference
cmdlet 参考文档
基本常识
- powershell 大小写不敏感,但是鼓励该大写的还是大写
- powershell的命令名是
动词-名称
形式的 - 通过
get-verb
可以获取powershell中的动词列表 - help 将帮助分页而get-help 不会分页,而只是将内容完全输出
获取相关命令
- 两种方式都支持模糊搜索(help/gcm)
- 我更喜欢用
help
,这是个更加通用的命令
方式1:get-help(help)
PS C:\Users\cxxu_11> help File
Name Category Module Synopsis
---- -------- ------ --------
New-PSSessionConfigurationFile Cmdlet Microsoft.PowerShell.Core Creates a file that…
Test-PSSessionConfigurationFile Cmdlet Microsoft.PowerShell.Core …
Write-CFile Function Carbon …
Convert-CXmlFile Function Carbon …
Test-CFileShare Function Carbon …
Read-CFile Function Carbon …
Uninstall-CFileShare Function Carbon …
Get-CFileSharePermission Function Carbon …
Get-CFileShare Function Carbon …
...
about_Session_Configuration_Files HelpFile
对比 get-command
PS C:\Users\cxxu_11> gcm *file*
CommandType Name Version Source
----------- ---- ------- ------
Function Add-PoshGitToProfile 1.0.0 posh-git
Function ciscoFiles 0.0 jumpDirecto…
Function Convert-CXmlFile 2.11.0 Carbon
Function FileInfo 1.0.0.0 PSColor
Function fritzingFiles 0.0
Function qqFiles 0.0 jumpDirecto…
Function Read-CFile 2.11.0 Carbon
Function Reset-CHostsFile 2.11.0 Carbon
...
1.0.0.1 PowerShellG…
Function wechatFiles 0.0 jumpDirecto…
Function WINDOWS\…
Application openfiles.exe 10.0.2200… C:\WINDOWS\…
Application profiler.bat 0.0.0.0 D:\exes\and…
Application profiler.exe 0.0.0.0 D:\exes\and…
方式2:get-command(gcm)
比如要获取和进程(process)相关的命令:
Get-Command *process*
这里的*
表示通配符
powershell语言的语法帮助about_帮助系统
- 用help获取相关主题:
- 例如操作符相关语法:
PS C:\Users\cxxu_11> help operator
Name Category Module Synopsis
---- -------- ------ --------
about_Arithmetic_Operators HelpFile
about_Assignment_Operators HelpFile
about_Comparison_Operators HelpFile
about_Logical_Operators HelpFile
about_Operators HelpFile
about_Operator_Precedence HelpFile
about_Pipeline_Chain_Operators HelpFile
about_Type_Operators HelpFile
PS C:\Users\cxxu_11> help about_Operators
ABOUT_OPERATORS
Short description
Describes the operators that are supported by PowerShell.
...
获取/更新帮助手册
Update-Help
某些情况下,您可能会收到错误,那么可以考虑改为运行如下指令
或者如果你想看到所有的消息详情:
Update-Help -Force -Verbose -ErrorAction SilentlyContinue
可以安全地忽略这些错误。它们不会影响PS功能或使用。
Get-Module -ListAvailable | Where HelpInfoUri |Update-Help
获取某条命令的使用帮助/example
获取帮助:
help <commandName>
这里man可以替换help(两者都是Get-Help)的别名
<commandName> -?
cmdlet的帮助选项
-example
相比于linux中的man,pwsh的-example
选项算是一个亮点
比如获取Stop-Process 命令的相关帮助
Get-Help Stop-Process
获取相关用法例子-example
Get-Help Stop-Process -Examples
❯ Get-Help Stop-Process -Examples
NAME
Stop-Process
SYNOPSIS
Stops one or more running processes.
---------- Example 1: Stop all instances of a process ----------
PS C:\> Stop-Process -Name "notepad"
This command stops all instances of the Notepad process on the computer. Each instance of Notepad runs in its own process. It uses the Name parameter to specify the processes, all of
which have the same name. If you were to use the Id parameter to stop the same processes, you would have to list the process IDs of each instance of Notepad.
------- Example 2: Stop a specific instance of a process -------
PS C:\> Stop-Process -Id 3952 -Confirm -PassThru
Confirm
Are you sure you want to perform this action?
Performing operation "Stop-Process" on Target "notepad (3952)".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"):y
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
41 2 996 3212 31 3952 notepad
This command stops a particular instance of the Notepad process. It uses the process ID, 3952, to identify the process. The Confirm parameter directs PowerShell to prompt you before
it stops the process. Because the prompt includes the process namein addition to its ID, this is best practice. The PassThru parameter passes the process object to the formatter for
display. Without this parameter, there would be no display after a Stop-Process command.
--- Example 3: Stop a process and detect that it has stopped ---
PS C:\> calc
PS C:\> $p = Get-Process -Name "calc"
PS C:\> Stop-Process -InputObject $p
PS C:\> Get-Process | Where-Object {$_.HasExited}
获取对象类型/成员
Get-Member
以Get-Date 为例
Get-Date | `Get-Member`
但是,更有用的是get-Member
获取对象的类型,然后利用get-command
来查询可以操作对应类型的cmdlet
learn:发现对象
例如:
处理显示内容(使用格式设置和筛选)
learn link 类似于数据库,powershell提供了一些格式化返回内容的工具对象
操作原则:正确设置格式,将格式设置作为最后一项操作
鉴于筛选左侧在语句中是指尽可能早地筛选出内容,从右往左格式化数据在语句中就是指尽可能晚地格式化数据。 但为什么需要延迟设置格式? 原因是格式设置命令将对象更改为格式对象。 这意味着数据不再位于相同的属性和方法中。(格式设置会破坏你正在处理的对象。)
Get-Process | Where-Object CPU -gt 2 | Sort-Object CPU -Descending | Select-Object -First 3
上述命令分别涉及到记录筛选,排序以及列(投影),来获取更有针对性的查询(操作)结果
format-list &format-table
其他参考连接