0021_PowerShell命令
命令类型
内置命令cmdlet
cmdlet概述
cmdlet的属性和方法
查看自己Powershell版本Get-Host
获取所有命令Get-Command
查看命令作用Get-Help
获知最后执行命令的状态
计算命令执行时间Measure-Command
命令的别名Get-ChildItem
操作使用管理控制台历史命令Get-History
重定向输出Out-File
获取文件内容Get-Content
记录PowerShell会话全文
显示对象属性为列表或表格Format-List和Format-Table
PowerShell命令
参考:
命令类型
PowerShell命令有四种命令类型:cmdlet,Alias,Function,Application
Windows PowerShell命令类型
cmdlet
:内置命令Alias
:别名,可以用来缩短常见的较长的cmdlet,部分DOS命令会被解释为别名Function
:内置功能函数,用于简化cmdlet功能组合,也有部分DOS命令会被解释为函数Application
:应用程序,用来打开Windows的应用程序,也有部分DOS命令被解释为应用程序
内置命令cmdlet
cmdlet概述
内置的命令很多,比如
Get-Help
;
定义:
- Windows PowerShell的命令行
- 管理的最小单位
- 可以单独使用,也可以组合使用
构成:
- “动词-名词”结构,
比如
Get-Command
中: 动词:Get,名词:Command - tab键补齐机制
查询:(使用Get-Command
)
由于动词数量比名词少,所以通常情况下可以按照动词来获取命令.
比如:
# 查看动词为get的所有命令
Get-Command -Verb Get或者get # 动词不区分大小写
类型:
cmdlet的类型名为System.Management.Automation.CmdletInfo,包含下列属性和方法
cmdlet的属性和方法
运行这个命令,其中的CmdletInfo就是cmdlet的属性和方法
Get-Command | Get-Member # 获取所有命令--| 获取属性,
Name | MemberType | Definition |
---|---|---|
Equals | Method | bool Equals(System.Object obj) |
GetHashCode | Method | int GetHashCode() |
GetType | Method | type GetType() |
ToString | Method | string ToString() |
CommandType | Property | System.Management.Automation.CommandTypes CommandType {get;} |
DefaultParameterSet | Property | System.String DefaultParameterSet {get;} |
Definition | Property | System.String Definition {get;} |
HelpFile | Property | System.String HelpFile {get;} |
ImplementingType | Property | System.Type ImplementingType {get;} |
Module | Property | System.Management.Automation.PSModuleInfo Module {get;} |
ModuleName | Property | System.String ModuleName {get;} |
Name | Property | System.String Name {get;} |
Noun | Property | System.String Noun {get;} |
OutputType | Property | System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Management.Automation.PSTypeName, System.Management.Automation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] OutputType {get;} |
Parameters | Property | System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Management.Automation.ParameterMetadata, System.Management.Automation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] Parameters {get;} |
ParameterSets | Property | System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Management.Automation.CommandParameterSetInfo, System.Management.Automation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] ParameterSets {get;} |
PSSnapIn | Property | System.Management.Automation.PSSnapInInfo PSSnapIn {get;} |
Verb | Property | System.String Verb {get;} |
Visibility | Property | System.Management.Automation.SessionStateEntryVisibility Visibility {get;set;} |
DLL | ScriptProperty | System.Object DLL {get=$this.ImplementingType.Assembly.Location;} |
HelpUri | ScriptProperty | System.Object HelpUri {get=ProgressPreference.. |
HelpUri隐藏内容
System.Object HelpUri {get=$oldProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
try
{
if ($psversiontable.psversion.Major -lt 3)
{
# ok to cast CommandTypes enum to HelpCategory because string/indentifier for
# cmdlet,function,filter,alias,externalscript is identical.
# it is ok to fail for other enum values (i.e. for Application)
$commandName = $this.Name
if ($this.ModuleName)
{
$commandName = "{0}\{1}" -f $this.ModuleName,$commandName
}
$helpObject = get-help -Name $commandName -Category ([string]($this.CommandType)) -ErrorAction SilentlyContinue
# return first non-null uri (and try not to hit any strict mode things)
if ($helpObject -eq $null) { return $null }
if ($helpObject.psobject.properties['relatedLinks'] -eq $null) { return $null }
if ($helpObject.relatedLinks.psobject.properties['navigationLink'] -eq $null) { return $null }
$helpUri = [string]$( $helpObject.relatedLinks.navigationLink | %{ if ($_.psobject.properties['uri'] -ne $null) { $_.uri } } | ?{ $_ } | select -first 1 )
return $helpUri
}
else
{
[Microsoft.PowerShell.Commands.GetHelpCodeMethods]::GetHelpUri($this)
}
}
catch {}
finally
{
$ProgressPreference = $oldProgressPreference
};}
查看自己Powershell版本Get-Host
PS F:\test1\ddtest2> Get-Host
Name : ConsoleHost
Version : 5.1.18362.145 # 版本
InstanceId : e91ff846-5f9f-47b0-aac6-33010722c2b5
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : zh-CN
CurrentUICulture : zh-CN
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled : True
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
获取所有命令Get-Command
语法:
Get-Command [过滤类型 过滤条件] # 根据条件获取命令列表
过滤类型:
-Name 名字
-CommandType 类型
-Verb 动作
-Noun 名词
Get-Command | 对象 # 查看一个对象的属性和方法
Name | ModuleName | Help |
---|---|---|
Add-Computer | Microsoft.PowerShell.Management | help |
Add-Content | Microsoft.PowerShell.Management | help |
Add-History | Microsoft.PowerShell.Core | help |
Add-Member | Microsoft.PowerShell.Utility | help |
Add-PSSnapin | Microsoft.PowerShell.Core | help |
Add-Type | Microsoft.PowerShell.Utility | help |
Checkpoint-Computer | Microsoft.PowerShell.Management | help |
Clear-Content | Microsoft.PowerShell.Management | help |
Clear-EventLog | Microsoft.PowerShell.Management | help |
Clear-History | Microsoft.PowerShell.Core | help |
Clear-Item | Microsoft.PowerShell.Management | help |
Clear-ItemProperty | Microsoft.PowerShell.Management | help |
Clear-Variable | Microsoft.PowerShell.Utility | help |
Compare-Object | Microsoft.PowerShell.Utility | help |
Complete-Transaction | Microsoft.PowerShell.Management | help |
Connect-WSMan | Microsoft.WSMan.Management | help |
ConvertFrom-Csv | Microsoft.PowerShell.Utility | help |
ConvertFrom-SecureString | Microsoft.PowerShell.Security | help |
ConvertFrom-StringData | Microsoft.PowerShell.Utility | help |
Convert-Path | Microsoft.PowerShell.Management | help |
ConvertTo-Csv | Microsoft.PowerShell.Utility | help |
ConvertTo-Html | Microsoft.PowerShell.Utility | help |
ConvertTo-SecureString | Microsoft.PowerShell.Security | help |
ConvertTo-Xml | Microsoft.PowerShell.Utility | help |
Copy-Item | Microsoft.PowerShell.Management | help |
Copy-ItemProperty | Microsoft.PowerShell.Management | help |
Debug-Process | Microsoft.PowerShell.Management | help |
Disable-ComputerRestore | Microsoft.PowerShell.Management | help |
Disable-PSBreakpoint | Microsoft.PowerShell.Utility | help |
Disable-PSSessionConfiguration | Microsoft.PowerShell.Core | help |
Disable-WSManCredSSP | Microsoft.WSMan.Management | help |
Disconnect-WSMan | Microsoft.WSMan.Management | help |
Enable-ComputerRestore | Microsoft.PowerShell.Management | help |
Enable-PSBreakpoint | Microsoft.PowerShell.Utility | help |
Enable-PSRemoting | Microsoft.PowerShell.Core | help |
Enable-PSSessionConfiguration | Microsoft.PowerShell.Core | help |
Enable-WSManCredSSP | Microsoft.WSMan.Management | help |
Enter-PSSession | Microsoft.PowerShell.Core | help |
Exit-PSSession | Microsoft.PowerShell.Core | help |
Export-Alias | Microsoft.PowerShell.Utility | help |
Export-Clixml | Microsoft.PowerShell.Utility | help |
Export-Console | Microsoft.PowerShell.Core | help |
Export-Counter | Microsoft.PowerShell.Diagnostics | help |
Export-Csv | Microsoft.PowerShell.Utility | help |
Export-FormatData | Microsoft.PowerShell.Utility | help |
Export-ModuleMember | Microsoft.PowerShell.Core | help |
Export-PSSession | Microsoft.PowerShell.Utility | help |
ForEach-Object | Microsoft.PowerShell.Core | help |
Format-Custom | Microsoft.PowerShell.Utility | help |
Format-List | Microsoft.PowerShell.Utility | help |
Format-Table | Microsoft.PowerShell.Utility | help |
Format-Wide | Microsoft.PowerShell.Utility | help |
Get-Acl | Microsoft.PowerShell.Security | help |
Get-Alias | Microsoft.PowerShell.Utility | help |
Get-AuthenticodeSignature | Microsoft.PowerShell.Security | help |
Get-ChildItem | Microsoft.PowerShell.Management | help |
Get-Command | Microsoft.PowerShell.Core | help |
Get-ComputerRestorePoint | Microsoft.PowerShell.Management | help |
Get-Content | Microsoft.PowerShell.Management | help |
Get-Counter | Microsoft.PowerShell.Diagnostics | help |
Get-Credential | Microsoft.PowerShell.Security | help |
Get-Culture | Microsoft.PowerShell.Utility | help |
Get-Date | Microsoft.PowerShell.Utility | help |
Get-Event | Microsoft.PowerShell.Utility | help |
Get-EventLog | Microsoft.PowerShell.Management | help |
Get-EventSubscriber | Microsoft.PowerShell.Utility | help |
Get-ExecutionPolicy | Microsoft.PowerShell.Security | help |
Get-FormatData | Microsoft.PowerShell.Utility | help |
Get-Help | Microsoft.PowerShell.Core | help |
Get-History | Microsoft.PowerShell.Core | help |
Get-Host | Microsoft.PowerShell.Utility | help |
Get-HotFix | Microsoft.PowerShell.Management | help |
Get-Item | Microsoft.PowerShell.Management | help |
Get-ItemProperty | Microsoft.PowerShell.Management | help |
Get-Job | Microsoft.PowerShell.Core | help |
Get-Location | Microsoft.PowerShell.Management | help |
Get-Member | Microsoft.PowerShell.Utility | help |
Get-Module | Microsoft.PowerShell.Core | help |
Get-PfxCertificate | Microsoft.PowerShell.Security | help |
Get-Process | Microsoft.PowerShell.Management | help |
Get-PSBreakpoint | Microsoft.PowerShell.Utility | help |
Get-PSCallStack | Microsoft.PowerShell.Utility | help |
Get-PSDrive | Microsoft.PowerShell.Management | help |
Get-PSProvider | Microsoft.PowerShell.Management | help |
Get-PSSession | Microsoft.PowerShell.Core | help |
Get-PSSessionConfiguration | Microsoft.PowerShell.Core | help |
Get-PSSnapin | Microsoft.PowerShell.Core | help |
Get-Random | Microsoft.PowerShell.Utility | help |
Get-Service | Microsoft.PowerShell.Management | help |
Get-TraceSource | Microsoft.PowerShell.Utility | help |
Get-Transaction | Microsoft.PowerShell.Management | help |
Get-UICulture | Microsoft.PowerShell.Utility | help |
Get-Unique | Microsoft.PowerShell.Utility | help |
Get-Variable | Microsoft.PowerShell.Utility | help |
Get-WinEvent | Microsoft.PowerShell.Diagnostics | help |
Get-WmiObject | Microsoft.PowerShell.Management | help |
Get-WSManCredSSP | Microsoft.WSMan.Management | help |
Get-WSManInstance | Microsoft.WSMan.Management | help |
Group-Object | Microsoft.PowerShell.Utility | help |
Import-Alias | Microsoft.PowerShell.Utility | help |
Import-Clixml | Microsoft.PowerShell.Utility | help |
Import-Counter | Microsoft.PowerShell.Diagnostics | help |
Import-Csv | Microsoft.PowerShell.Utility | help |
Import-LocalizedData | Microsoft.PowerShell.Utility | help |
Import-Module | Microsoft.PowerShell.Core | help |
Import-PSSession | Microsoft.PowerShell.Utility | help |
Invoke-Command | Microsoft.PowerShell.Core | help |
Invoke-Expression | Microsoft.PowerShell.Utility | help |
Invoke-History | Microsoft.PowerShell.Core | help |
Invoke-Item | Microsoft.PowerShell.Management | help |
Invoke-WmiMethod | Microsoft.PowerShell.Management | help |
Invoke-WSManAction | Microsoft.WSMan.Management | help |
Join-Path | Microsoft.PowerShell.Management | help |
Limit-EventLog | Microsoft.PowerShell.Management | help |
Measure-Command | Microsoft.PowerShell.Utility | help |
Measure-Object | Microsoft.PowerShell.Utility | help |
Move-Item | Microsoft.PowerShell.Management | help |
Move-ItemProperty | Microsoft.PowerShell.Management | help |
New-Alias | Microsoft.PowerShell.Utility | help |
New-Event | Microsoft.PowerShell.Utility | help |
New-EventLog | Microsoft.PowerShell.Management | help |
New-Item | Microsoft.PowerShell.Management | help |
New-ItemProperty | Microsoft.PowerShell.Management | help |
New-Module | Microsoft.PowerShell.Core | help |
New-ModuleManifest | Microsoft.PowerShell.Core | help |
New-Object | Microsoft.PowerShell.Utility | help |
New-PSDrive | Microsoft.PowerShell.Management | help |
New-PSSession | Microsoft.PowerShell.Core | help |
New-PSSessionOption | Microsoft.PowerShell.Core | help |
New-Service | Microsoft.PowerShell.Management | help |
New-TimeSpan | Microsoft.PowerShell.Utility | help |
New-Variable | Microsoft.PowerShell.Utility | help |
New-WebServiceProxy | Microsoft.PowerShell.Management | help |
New-WSManInstance | Microsoft.WSMan.Management | help |
New-WSManSessionOption | Microsoft.WSMan.Management | help |
Out-Default | Microsoft.PowerShell.Utility | help |
Out-File | Microsoft.PowerShell.Utility | help |
Out-GridView | Microsoft.PowerShell.Utility | help |
Out-Host | Microsoft.PowerShell.Utility | help |
Out-Null | Microsoft.PowerShell.Utility | help |
Out-Printer | Microsoft.PowerShell.Utility | help |
Out-String | Microsoft.PowerShell.Utility | help |
Pop-Location | Microsoft.PowerShell.Management | help |
Push-Location | Microsoft.PowerShell.Management | help |
Read-Host | Microsoft.PowerShell.Utility | help |
Receive-Job | Microsoft.PowerShell.Core | help |
Register-EngineEvent | Microsoft.PowerShell.Utility | help |
Register-ObjectEvent | Microsoft.PowerShell.Utility | help |
Register-PSSessionConfiguration | Microsoft.PowerShell.Core | help |
Register-WmiEvent | Microsoft.PowerShell.Management | help |
Remove-Computer | Microsoft.PowerShell.Management | help |
Remove-Event | Microsoft.PowerShell.Utility | help |
Remove-EventLog | Microsoft.PowerShell.Management | help |
Remove-Item | Microsoft.PowerShell.Management | help |
Remove-ItemProperty | Microsoft.PowerShell.Management | help |
Remove-Job | Microsoft.PowerShell.Core | help |
Remove-Module | Microsoft.PowerShell.Core | help |
Remove-PSBreakpoint | Microsoft.PowerShell.Utility | help |
Remove-PSDrive | Microsoft.PowerShell.Management | help |
Remove-PSSession | Microsoft.PowerShell.Core | help |
Remove-PSSnapin | Microsoft.PowerShell.Core | help |
Remove-Variable | Microsoft.PowerShell.Utility | help |
Remove-WmiObject | Microsoft.PowerShell.Management | help |
Remove-WSManInstance | Microsoft.WSMan.Management | help |
Rename-Item | Microsoft.PowerShell.Management | help |
Rename-ItemProperty | Microsoft.PowerShell.Management | help |
Reset-ComputerMachinePassword | Microsoft.PowerShell.Management | help |
Resolve-Path | Microsoft.PowerShell.Management | help |
Restart-Computer | Microsoft.PowerShell.Management | help |
Restart-Service | Microsoft.PowerShell.Management | help |
Restore-Computer | Microsoft.PowerShell.Management | help |
Resume-Service | Microsoft.PowerShell.Management | help |
Select-Object | Microsoft.PowerShell.Utility | help |
Select-String | Microsoft.PowerShell.Utility | help |
Select-Xml | Microsoft.PowerShell.Utility | help |
Send-MailMessage | Microsoft.PowerShell.Utility | help |
Set-Acl | Microsoft.PowerShell.Security | help |
Set-Alias | Microsoft.PowerShell.Utility | help |
Set-AuthenticodeSignature | Microsoft.PowerShell.Security | help |
Set-Content | Microsoft.PowerShell.Management | help |
Set-Date | Microsoft.PowerShell.Utility | help |
Set-ExecutionPolicy | Microsoft.PowerShell.Security | help |
Set-Item | Microsoft.PowerShell.Management | help |
Set-ItemProperty | Microsoft.PowerShell.Management | help |
Set-Location | Microsoft.PowerShell.Management | help |
Set-PSBreakpoint | Microsoft.PowerShell.Utility | help |
Set-PSDebug | Microsoft.PowerShell.Core | help |
Set-PSSessionConfiguration | Microsoft.PowerShell.Core | help |
Set-Service | Microsoft.PowerShell.Management | help |
Set-StrictMode | Microsoft.PowerShell.Core | help |
Set-TraceSource | Microsoft.PowerShell.Utility | help |
Set-Variable | Microsoft.PowerShell.Utility | help |
Set-WmiInstance | Microsoft.PowerShell.Management | help |
Set-WSManInstance | Microsoft.WSMan.Management | help |
Set-WSManQuickConfig | Microsoft.WSMan.Management | help |
Show-EventLog | Microsoft.PowerShell.Management | help |
Sort-Object | Microsoft.PowerShell.Utility | help |
Split-Path | Microsoft.PowerShell.Management | help |
Start-Job | Microsoft.PowerShell.Core | help |
Start-Process | Microsoft.PowerShell.Management | help |
Start-Service | Microsoft.PowerShell.Management | help |
Start-Sleep | Microsoft.PowerShell.Utility | help |
Start-Transaction | Microsoft.PowerShell.Management | help |
Start-Transcript | Microsoft.PowerShell.Host | help |
Stop-Computer | Microsoft.PowerShell.Management | help |
Stop-Job | Microsoft.PowerShell.Core | help |
Stop-Process | Microsoft.PowerShell.Management | help |
Stop-Service | Microsoft.PowerShell.Management | help |
Stop-Transcript | Microsoft.PowerShell.Host | help |
Suspend-Service | Microsoft.PowerShell.Management | help |
Tee-Object | Microsoft.PowerShell.Utility | help |
Test-ComputerSecureChannel | Microsoft.PowerShell.Management | help |
Test-Connection | Microsoft.PowerShell.Management | help |
Test-ModuleManifest | Microsoft.PowerShell.Core | help |
Test-Path | Microsoft.PowerShell.Management | help |
Test-WSMan | Microsoft.WSMan.Management | help |
Trace-Command | Microsoft.PowerShell.Utility | help |
Undo-Transaction | Microsoft.PowerShell.Management | help |
Unregister-Event | Microsoft.PowerShell.Utility | help |
Unregister-PSSessionConfiguration | Microsoft.PowerShell.Core | help |
Update-FormatData | Microsoft.PowerShell.Utility | help |
Update-List | Microsoft.PowerShell.Utility | help |
Update-TypeData | Microsoft.PowerShell.Utility | help |
Use-Transaction | Microsoft.PowerShell.Management | help |
Wait-Event | Microsoft.PowerShell.Utility | help |
Wait-Job | Microsoft.PowerShell.Core | help |
Wait-Process | Microsoft.PowerShell.Management | help |
Where-Object | Microsoft.PowerShell.Core | help |
Write-Debug | Microsoft.PowerShell.Utility | help |
Write-Error | Microsoft.PowerShell.Utility | help |
Write-EventLog | Microsoft.PowerShell.Management | help |
Write-Host | Microsoft.PowerShell.Utility | help |
Write-Output | Microsoft.PowerShell.Utility | help |
Write-Progress | Microsoft.PowerShell.Utility | help |
Write-Verbose | Microsoft.PowerShell.Utility | help |
Write-Warning | Microsoft.PowerShell.Utility | help |
比如:
获取列表:
Get-Command # 获取所有命令
Get-Command -Name *名字* # 获取name为这个匹配的所有的命令
Get-Command -CommandType "类型" # 获取这个类型的命令(类型共四种)
Get-Command -Verb 动作 # 获取这个动作的命令(-*的所有命令,不区分大小写)
Get-Command -Noun 名词 # 获取这个名词的命令(*-名词的命令,不区分大小写))
# 比如 Get-Command -Noun Command 就可以获得 *-Command 等的命令
看一个对象的属性和方法:
Get-Command | Get-Member
查看命令作用Get-Help
Get-Help *命令* [可选参数] # 查看一个命令的作用
可选参数:
-Detailed # 查看详细信息
-Full # 查看技术支持信息,可以当接口文档用了(比Detailed全)
-Examples #查看实例
-
# 命令的名字可以模糊查询
比如:获取命令help
PS F:\test1\ddtest2> Get-Help Get-Command
名称
Get-Command
语法
Get-Command [[-ArgumentList] <Object[]>] [<CommonParameters>]
Get-Command [[-Name] <string[]>] [[-ArgumentList] <Object[]>] [<CommonParameters>]
别名
gcm
备注
Get-Help 在此计算机上找不到该 cmdlet 的帮助文件。它仅显示部分帮助。
-- 若要下载并安装包含此 cmdlet 的模块的帮助文件,请使用 Update-Help。
-- 若要联机查看此 cmdlet 的帮助主题,请键入: "Get-Help Get-Command -Online" 或
转到 https://go.microsoft.com/fwlink/?LinkID=113309。
比如:模糊获取
PS F:\test1\ddtest2> Get-Help *comm*
Name Category Module Synopsis
---- -------- ------ --------
Get-Command Cmdlet Microsoft.PowerShell.Core ...
Invoke-Command Cmdlet Microsoft.PowerShell.Core ...
Measure-Command Cmdlet Microsoft.PowerShell.U... ...
Show-Command Cmdlet Microsoft.PowerShell.U... ...
Trace-Command Cmdlet Microsoft.PowerShell.U... ...
Invoke-CommandInDesktopPackage Cmdlet Appx ...
SafeGetCommand Function Pester ...
Find-Command Function PowerShellGet ...
比如:-Detailed
PS F:\test1\ddtest2> Get-Help Get-Command -Detailed
名称
Get-Command
语法
Get-Command [[-ArgumentList] <Object[]>] [<CommonParameters>]
Get-Command [[-Name] <string[]>] [[-ArgumentList] <Object[]>] [<CommonParameters>]
参数
-All
-ArgumentList <Object[]>
-CommandType <CommandTypes>
-FullyQualifiedModule <ModuleSpecification[]>
-ListImported
-Module <string[]>
-Name <string[]>
-Noun <string[]>
-ParameterName <string[]>
-ParameterType <PSTypeName[]>
-ShowCommandInfo
-Syntax
-TotalCount <int>
-Verb <string[]>
<CommonParameters>
此 Cmdlet 支持常见参数: Verbose、Debug、
ErrorAction、ErrorVariable、WarningAction、WarningVariable、
OutBuffer、PipelineVariable 和 OutVariable。有关详细信息,请参阅
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216)。
别名
gcm
备注
Get-Help 在此计算机上找不到该 cmdlet 的帮助文件。它仅显示部分帮助。
-- 若要下载并安装包含此 cmdlet 的模块的帮助文件,请使用 Update-Help。
-- 若要联机查看此 cmdlet 的帮助主题,请键入: "Get-Help Get-Command -Online" 或
转到 https://go.microsoft.com/fwlink/?LinkID=113309。
比如:-Full
PS F:\test1\ddtest2> Get-Help Get-Command -Full
名称
Get-Command
语法
Get-Command [[-ArgumentList] <Object[]>] [<CommonParameters>]
Get-Command [[-Name] <string[]>] [[-ArgumentList] <Object[]>] [<CommonParameters>]
参数
-All
是否必需? False
位置? 已命名
是否接受管道输入? True (ByPropertyName)
参数集名称 (所有)
别名 无
动态? false
-ArgumentList <Object[]>
是否必需? False
位置? 1
是否接受管道输入? True (FromRemainingArguments)
参数集名称 (所有)
别名 Args
动态? false
-CommandType <CommandTypes>
是否必需? False
位置? 已命名
是否接受管道输入? True (ByPropertyName)
参数集名称 AllCommandSet
别名 Type
动态? false
-FullyQualifiedModule <ModuleSpecification[]>
是否必需? False
位置? 已命名
是否接受管道输入? True (ByPropertyName)
参数集名称 (所有)
别名 无
动态? false
-ListImported
是否必需? False
位置? 已命名
是否接受管道输入? True (ByPropertyName)
参数集名称 (所有)
别名 无
动态? false
-Module <string[]>
是否必需? False
位置? 已命名
是否接受管道输入? True (ByPropertyName)
参数集名称 (所有)
别名 PSSnapin
动态? false
-Name <string[]>
是否必需? False
位置? 0
是否接受管道输入? True (ByValue, ByPropertyName)
参数集名称 AllCommandSet
别名 无
动态? false
-Noun <string[]>
是否必需? False
位置? 已命名
是否接受管道输入? True (ByPropertyName)
参数集名称 CmdletSet
别名 无
动态? false
-ParameterName <string[]>
是否必需? False
位置? 已命名
是否接受管道输入? False
参数集名称 (所有)
别名 无
动态? false
-ParameterType <PSTypeName[]>
是否必需? False
位置? 已命名
是否接受管道输入? False
参数集名称 (所有)
别名 无
动态? false
-ShowCommandInfo
是否必需? False
位置? 已命名
是否接受管道输入? False
参数集名称 (所有)
别名 无
动态? false
-Syntax
是否必需? False
位置? 已命名
是否接受管道输入? True (ByPropertyName)
参数集名称 (所有)
别名 无
动态? false
-TotalCount <int>
是否必需? False
位置? 已命名
是否接受管道输入? True (ByPropertyName)
参数集名称 (所有)
别名 无
动态? false
-Verb <string[]>
是否必需? False
位置? 已命名
是否接受管道输入? True (ByPropertyName)
参数集名称 CmdletSet
别名 无
动态? false
<CommonParameters>
此 Cmdlet 支持常见参数: Verbose、Debug、
ErrorAction、ErrorVariable、WarningAction、WarningVariable、
OutBuffer、PipelineVariable 和 OutVariable。有关详细信息,请参阅
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216)。
输入
System.String[]
Microsoft.PowerShell.Commands.ModuleSpecification[]
System.Management.Automation.CommandTypes
System.Int32
System.Management.Automation.SwitchParameter
System.Object[]
输出
System.Management.Automation.AliasInfo
System.Management.Automation.ApplicationInfo
System.Management.Automation.FunctionInfo
System.Management.Automation.CmdletInfo
System.Management.Automation.ExternalScriptInfo
System.Management.Automation.FilterInfo
System.Management.Automation.WorkflowInfo
System.String
System.Management.Automation.PSObject
别名
gcm
备注
Get-Help 在此计算机上找不到该 cmdlet 的帮助文件。它仅显示部分帮助。
-- 若要下载并安装包含此 cmdlet 的模块的帮助文件,请使用 Update-Help。
-- 若要联机查看此 cmdlet 的帮助主题,请键入: "Get-Help Get-Command -Online" 或
转到 https://go.microsoft.com/fwlink/?LinkID=113309。
比如:-Examples
PS F:\test1\ddtest2> Get-Help Get-Command -Examples
名称
Get-Command
别名
gcm
备注
Get-Help 在此计算机上找不到该 cmdlet 的帮助文件。它仅显示部分帮助。
-- 若要下载并安装包含此 cmdlet 的模块的帮助文件,请使用 Update-Help。
-- 若要联机查看此 cmdlet 的帮助主题,请键入: "Get-Help Get-Command -Online" 或
转到 https://go.microsoft.com/fwlink/?LinkID=113309。
获知最后执行命令的状态
PowerShell提供了两个变量可以检测最后执行的命令是否成功:$lastExitCode
和$?
。
$lastExitCode
:数字型变量,返回最后脚本或应用程序执行返回的退出码或出错级别(没有错误返回空)
$?
:布尔型变量,返回最后执行命令的成功还是失败:
变量$?
使用更通用的方式来描述最后的应用程序退出的状态,在以下应用程序发生错误的时候,PowerShell会设置$?
为False:
- 应用程序退出码非零;
- cmdlet或脚本输出错误信息;
- cmdlet或脚本捕获到终止错误或异常。
当命令执行没有错误的时候,PowerShell设置$?变量为True。
计算命令执行时间Measure-Command
如果想计算一个命令执行时间,可以使用Measure-Command
命令
PS C:\Users\jsy1> Measure-Command {ping 127.0.0.1}
Days : 0
Hours : 0
Minutes : 0
Seconds : 3
Milliseconds : 36
Ticks : 30368179
TotalDays : 3.51483553240741E-05
TotalHours : 0.000843560527777778
TotalMinutes : 0.0506136316666667
TotalSeconds : 3.0368179
TotalMilliseconds : 3036.8179
命令的别名Get-ChildItem
PowerShell一些内置命令都有别名,方便记忆和输入,可以用Get-Help
命令查看别名,如下Get-ChildItem
的命令有三个别名:gci
、ls
和dir
,输入任意一个都可以列举当前目录。
PS F:\test1\ddtest2> Get-Help Get-ChildItem
名称
Get-ChildItem
语法
Get-ChildItem [[-Path] <string[]>] [[-Filter] <string>] [<CommonParameters>]
Get-ChildItem [[-Filter] <string>] [<CommonParameters>]
别名
gci
ls
dir
备注
Get-Help 在此计算机上找不到该 cmdlet 的帮助文件。它仅显示部分帮助。
-- 若要下载并安装包含此 cmdlet 的模块的帮助文件,请使用 Update-Help。
-- 若要联机查看此 cmdlet 的帮助主题,请键入: "Get-Help Get-ChildItem -Online" 或
转到 https://go.microsoft.com/fwlink/?LinkID=113308。
操作使用管理控制台历史命令Get-History
在PowerShell窗口中,按上下箭头键可以寻找历史命令进行调用,也可以运行Get-History
命令查找,用Invoke-History Id
方式进行调用
PS C:\Users\jsy1> Get-History
Id CommandLine
-- -----------
1 Get-Help Get-ChildItem
2 Measure-Command ping 127.0.0.1
3 Measure-Command {ping 127.0.0.1}
4 ls
5 ll
6 ls
PS C:\Users\jsy1> Invoke-History 3
Measure-Command {ping 127.0.0.1}
Days : 0
Hours : 0
Minutes : 0
Seconds : 3
Milliseconds : 20
Ticks : 30202975
TotalDays : 3.49571469907407E-05
TotalHours : 0.000838971527777778
TotalMinutes : 0.0503382916666667
TotalSeconds : 3.0202975
TotalMilliseconds : 3020.2975
重定向输出Out-File
如果想把得到的结果输出到文件中,可以使用Out-File命令或者重定向操作符将命令输出的结果保存在文件中:
语法
Out-File [-FilePath] <string> [[-Encoding] {unknown | string | unicode | bigendianunicode | utf8 | utf7 | utf32 | ascii | default | oem}] [<CommonParameters>]
Out-File [[-Encoding] {unknown | string | unicode | bigendianunicode | utf8 | utf7 | utf32 | ascii | default | oem}] [<CommonParameters>]
案例
PS F:\test1\ddtest2> Out-File .\text1.txt
PS F:\test1\ddtest2> ls
目录: F:\test1\ddtest2
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/8/13 8:51 2 text1.txt
在这个文件夹下,会生成一个text1.txt文件,内容为空
获取文件内容Get-Content
语法
Get-Content [-Path] <string[]> [<CommonParameters>]
Get-Content [<CommonParameters>]
别名
gc
cat
type
例如:
PS F:\test1\ddtest2> "Powershell Routing" >test.txt
PS F:\test1\ddtest2> Get-Content .\test.txt
Powershell Routing
记录PowerShell会话全文
- 开始记录:
Start-Transcript
- 结束记录:
Stop-Transcript
如果想生成当前会话的记录,可以运行Start-Transcript
命令,它基于当前系统时间。如果想停止,运行Stop-Transcript
:
PS F:\test1\ddtest2> Start-Transcript -Path h.txt 已启动脚本,输出文件为 h.txt
PS F:\test1\ddtest2> Get-Command -Name *cess
CommandType Name Version Source
----------- ---- ------- ------
Function Block-FileShareAccess 2.0.0.0 Storage
Function Block-SmbShareAccess 2.0.0.0 SmbShare
Function Clear-AssignedAccess 1.0.0.0 AssignedAccess
Function Get-AppvVirtualProcess 1.0.0.0 AppvClient
Function Get-AssignedAccess 1.0.0.0 AssignedAccess
Function Get-SmbShareAccess 2.0.0.0 SmbShare
Function Grant-FileShareAccess 2.0.0.0 Storage
Function Grant-SmbShareAccess 2.0.0.0 SmbShare
Function Revoke-FileShareAccess 2.0.0.0 Storage
Function Revoke-SmbShareAccess 2.0.0.0 SmbShare
Function Set-AssignedAccess 1.0.0.0 AssignedAccess
Function Start-AppvVirtualProcess 1.0.0.0 AppvClient
Function Unblock-FileShareAccess 2.0.0.0 Storage
Function Unblock-SmbShareAccess 2.0.0.0 SmbShare
Cmdlet Debug-Process 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Enter-PSHostProcess 3.0.0.0 Microsoft.PowerShell.Core
Cmdlet Exit-PSHostProcess 3.0.0.0 Microsoft.PowerShell.Core
Cmdlet Get-Process 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Start-Process 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Stop-Process 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Wait-Process 3.1.0.0 Microsoft.PowerShell.Management
PS F:\test1\ddtest2> Get-Help Get-Variable
名称
Get-Variable
语法
Get-Variable [[-Name] <string[]>] [<CommonParameters>]
别名
gv
备注
Get-Help 在此计算机上找不到该 cmdlet 的帮助文件。它仅显示部分帮助。
-- 若要下载并安装包含此 cmdlet 的模块的帮助文件,请使用 Update-Help。
-- 若要联机查看此 cmdlet 的帮助主题,请键入: "Get-Help Get-Variable -Online" 或
转到 https://go.microsoft.com/fwlink/?LinkID=113336。
PS F:\test1\ddtest2> Stop-Transcript 已停止脚本,输出文件为 F:\test1\ddtest2\h.txt
输出的文件为
**********************
Windows PowerShell 脚本开始
开始时间: 20200813090647
用户名: DESKTOP-TPFM5VI\jsy1
RunAs 用户: DESKTOP-TPFM5VI\jsy1
配置名称:
计算机: DESKTOP-TPFM5VI (Microsoft Windows NT 10.0.18363.0)
主机应用程序: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
进程 ID: 6636
PSVersion: 5.1.18362.145
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.18362.145
BuildVersion: 10.0.18362.145
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
已启动脚本,输出文件为 h.txt
PS F:\test1\ddtest2> Get-Command -Name *cess
CommandType Name Version Source
----------- ---- ------- ------
Function Block-FileShareAccess 2.0.0.0 Storage
Function Block-SmbShareAccess 2.0.0.0 SmbShare
Function Clear-AssignedAccess 1.0.0.0 AssignedAccess
Function Get-AppvVirtualProcess 1.0.0.0 AppvClient
Function Get-AssignedAccess 1.0.0.0 AssignedAccess
Function Get-SmbShareAccess 2.0.0.0 SmbShare
Function Grant-FileShareAccess 2.0.0.0 Storage
Function Grant-SmbShareAccess 2.0.0.0 SmbShare
Function Revoke-FileShareAccess 2.0.0.0 Storage
Function Revoke-SmbShareAccess 2.0.0.0 SmbShare
Function Set-AssignedAccess 1.0.0.0 AssignedAccess
Function Start-AppvVirtualProcess 1.0.0.0 AppvClient
Function Unblock-FileShareAccess 2.0.0.0 Storage
Function Unblock-SmbShareAccess 2.0.0.0 SmbShare
Cmdlet Debug-Process 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Enter-PSHostProcess 3.0.0.0 Microsoft.PowerShell.Core
Cmdlet Exit-PSHostProcess 3.0.0.0 Microsoft.PowerShell.Core
Cmdlet Get-Process 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Start-Process 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Stop-Process 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Wait-Process 3.1.0.0 Microsoft.PowerShell.Management
PS F:\test1\ddtest2> Get-Help Get-Variable
名称
Get-Variable
语法
Get-Variable [[-Name] <string[]>] [<CommonParameters>]
别名
gv
备注
Get-Help 在此计算机上找不到该 cmdlet 的帮助文件。它仅显示部分帮助。
-- 若要下载并安装包含此 cmdlet 的模块的帮助文件,请使用 Update-Help。
-- 若要联机查看此 cmdlet 的帮助主题,请键入: "Get-Help Get-Variable -Online" 或
转到 https://go.microsoft.com/fwlink/?LinkID=113336。
PS F:\test1\ddtest2> Stop-Transcript
**********************
Windows PowerShell 脚本结束
结束时间: 20200813090750
**********************
显示对象属性为列表或表格Format-List和Format-Table
默认是Format-Table的
语法:
命令 | Format-List或Format-Table
案例:
# 当前文件夹中有两个文件h.txt和text1.txt
# 原始命令
PS F:\test1\ddtest2> ls
目录: F:\test1\ddtest2
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/8/13 9:07 3598 h.txt
-a---- 2020/8/13 8:51 2 text1.txt
# 显示为list
PS F:\test1\ddtest2> ls | Format-List
目录: F:\test1\ddtest2
Name : h.txt
Length : 3598
CreationTime : 2020/8/13 9:06:47
LastWriteTime : 2020/8/13 9:07:50
LastAccessTime : 2020/8/13 9:07:50
Mode : -a----
LinkType :
Target : {}
VersionInfo : File: F:\test1\ddtest2\h.txt
InternalName:
OriginalFilename:
FileVersion:
FileDescription:
Product:
ProductVersion:
Debug: False
Patched: False
PreRelease: False
PrivateBuild: False
SpecialBuild: False
Language:
Name : text1.txt
Length : 2
CreationTime : 2020/8/13 8:51:06
LastWriteTime : 2020/8/13 8:51:06
LastAccessTime : 2020/8/13 8:51:06
Mode : -a----
LinkType :
Target : {}
VersionInfo : File: F:\test1\ddtest2\text1.txt
InternalName:
OriginalFilename:
FileVersion:
FileDescription:
Product:
ProductVersion:
Debug: False
Patched: False
PreRelease: False
PrivateBuild: False
SpecialBuild: False
Language:
# 显示table(默认)
PS F:\test1\ddtest2> ls | Format-Table
目录: F:\test1\ddtest2
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/8/13 9:07 3598 h.txt
-a---- 2020/8/13 8:51 2 text1.txt
# table展示,限制显示字段
PS F:\test1\ddtest2> ls | Format-Table Name,Length
Name Length
---- ------
h.txt 3598
text1.txt 2