$PSScriptRoot.GetType().FullName
> System.String
$PSScriptRoot
> C:\Temp
$PSScriptRoot
是一个自动变量,它只保存当前脚本目录的字符串对象。
$MyInvocation.GetType().FullName
> System.Management.Automation.InvocationInfo
$MyInvocation
> MyCommand : test.ps1
> BoundParameters : {}
> UnboundArguments : {}
> ScriptLineNumber : 0
> OffsetInLine : 0
> HistoryId : 4
> ScriptName :
> Line :
> PositionMessage :
> PSScriptRoot :
> PSCommandPath :
> InvocationName : C:\Temp\test.ps1
> PipelineLength : 2
> PipelinePosition : 1
> ExpectingInput : False
> CommandOrigin : Internal
> DisplayScriptPosition :
$MyInvocation | Get-Member -Force
TypeName: System.Management.Automation.InvocationInfo
Name MemberType Definition
---- ---------- ----------
pstypenames CodeProperty System.Collections.ObjectModel.Collection`1[[System.String, mscorlib...
psadapted MemberSet psadapted {MyCommand, BoundParameters, UnboundArguments, ScriptLineN...
psbase MemberSet psbase {MyCommand, BoundParameters, UnboundArguments, ScriptLineNumb...
psextended MemberSet psextended {}
psobject MemberSet psobject {BaseObject, Members, Properties, Methods, ImmediateBaseObj...
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
get_BoundParameters Method System.Collections.Generic.Dictionary[string,System.Object] get_Boun...
get_CommandOrigin Method System.Management.Automation.CommandOrigin get_CommandOrigin()
get_DisplayScriptPosition Method System.Management.Automation.Language.IScriptExtent get_DisplayScrip...
get_ExpectingInput Method bool get_ExpectingInput()
get_HistoryId Method long get_HistoryId()
get_InvocationName Method string get_InvocationName()
get_Line Method string get_Line()
get_MyCommand Method System.Management.Automation.CommandInfo get_MyCommand()
get_OffsetInLine Method int get_OffsetInLine()
get_PipelineLength Method int get_PipelineLength()
get_PipelinePosition Method int get_PipelinePosition()
get_PositionMessage Method string get_PositionMessage()
get_PSCommandPath Method string get_PSCommandPath()
get_PSScriptRoot Method string get_PSScriptRoot()
get_ScriptLineNumber Method int get_ScriptLineNumber()
get_ScriptName Method string get_ScriptName()
get_UnboundArguments Method System.Collections.Generic.List[System.Object] get_UnboundArguments()
set_DisplayScriptPosition Method void set_DisplayScriptPosition(System.Management.Automation.Language...
ToString Method string ToString()
BoundParameters Property System.Collections.Generic.Dictionary[string,System.Object] BoundPar...
CommandOrigin Property System.Management.Automation.CommandOrigin CommandOrigin {get;}
DisplayScriptPosition Property System.Management.Automation.Language.IScriptExtent DisplayScriptPos...
ExpectingInput Property bool ExpectingInput {get;}
HistoryId Property long HistoryId {get;}
InvocationName Property string InvocationName {get;}
Line Property string Line {get;}
MyCommand Property System.Management.Automation.CommandInfo MyCommand {get;}
OffsetInLine Property int OffsetInLine {get;}
PipelineLength Property int PipelineLength {get;}
PipelinePosition Property int PipelinePosition {get;}
PositionMessage Property string PositionMessage {get;}
PSCommandPath Property string PSCommandPath {get;}
PSScriptRoot Property string PSScriptRoot {get;}
ScriptLineNumber Property int ScriptLineNumber {get;}
ScriptName Property string ScriptName {get;}
UnboundArguments Property System.Collections.Generic.List[System.Object] UnboundArguments {get;}
Function Example { $MyInvocation } Example
MyCommand : Example
BoundParameters : {}
UnboundArguments : {}
ScriptLineNumber : 8
OffsetInLine : 1
HistoryId : 6
ScriptName : C:\Temp\test.ps1
Line : Example
PositionMessage : At C:\Temp\test.ps1:8 char:1
+ Example
+ ~~~~~~~
PSScriptRoot : C:\Temp
PSCommandPath : C:\Temp\test.ps1
InvocationName : Example
PipelineLength : 1
PipelinePosition : 1
ExpectingInput : False
CommandOrigin : Internal
DisplayScriptPosition :
$MyInvocation
是为每个作用域生成的具有非常不同类型的自动变量。它的成员和效用因范围而异。
在 PSv5.1、Windows 7 SP1 上完成的测试