Get-Location

获取有关当前工作位置或位置堆栈的信息。

语法

Location (默认值)

Get-Location
    [-PSProvider <String[]>]
    [-PSDrive <String[]>]
    [-UseTransaction]
    [<CommonParameters>]

Stack

Get-Location
    [-Stack]
    [-StackName <String[]>]
    [-UseTransaction]
    [<CommonParameters>]

说明

Get-Location cmdlet 获取表示当前目录的对象,这与打印工作目录 (pwd) 命令非常相似。

在 Windows PowerShell 驱动器之间移动时,Windows PowerShell 会在每个驱动器中保留位置。 可以使用此 cmdlet 查找每个驱动器中的位置。

您可以使用此 cmdlet 在运行时获取当前目录,并在函数和脚本中使用它,例如在在 Windows PowerShell 提示符中显示当前目录的函数中。

还可以使用此 cmdlet 在位置堆栈中显示位置。 有关详细信息,请参阅 StackStackName 参数的说明和说明。

示例

示例 1:显示当前驱动器位置

PS C:\> Get-Location
Path
----
C:\WINDOWS

此命令在当前 Windows PowerShell 驱动器中显示你的位置。

例如,如果您位于 C: 驱动器的 Windows 目录中,它会显示该目录的路径。

示例 2:显示不同驱动器的当前位置

The first command uses the **Set-Location** cmdlet to set the current location to the Windows subdirectory of the C: drive.
PS C:\> Set-Location C:\Windows

The second command uses the **Set-Location** cmdlet to change the location to the HKLM:\Software\Microsoft registry key. When you change to a location in the HKLM: drive, Windows PowerShell retains your location in the C: drive.
PS C:\>
PS C:\WINDOWS> Set-Location HKLM:\Software\Microsoft
PS HKLM:\Software\Microsoft>

The third command uses the **Set-Location** cmdlet to change the location to the HKCU:\Control Panel\Input Method registry key.
PS C:\>
PS HKLM:\Software\Microsoft> Set-Location "HKCU:\Control Panel\Input Method"
PS HKCU:\Control Panel\Input Method>

The fourth command uses the **Get-Location** cmdlet to find the current location on the C: drive. It uses the *PSDrive* parameter to specify the drive.
PS C:\>
PS HKCU:\Control Panel\Input Method> Get-Location -PSDrive C



Path
----
C:\WINDOWS

The fifth command uses the **Set-Location** cmdlet to return to the C: drive. Even though the command does not specify a subdirectory, Windows PowerShell returns you to the saved location.
PS C:\>
PS HKCU:\Control Panel\Input Method> Set-Location C:
PS C:\WINDOWS>

The sixth command uses the **Get-Location** cmdlet to find the current location in the drives supported by the Windows PowerShell registry provider. **Get-Location** returns the location of the most recently accessed registry drive, HKCU.
PS C:\>
PS C:\WINDOWS> Get-Location -PSProvider registry




Path
----
HKCU:\Control Panel\Input Method

To see the current location in the HKLM: drive, you need to use the *PSDrive* parameter to specify the drive. The seventh command does just this:
PS C:\>
PS C:\WINDOWS> Get-Location -PSDrive HKLM



Path
----
HKLM:\Software\Microsoft

此示例演示如何使用 Get-Location 在不同的 Windows PowerShell 驱动器中显示您的当前位置。

示例 3:列出当前位置堆栈和备用位置堆栈中的位置

The first command sets the current location to the Windows directory on the C: drive.
PS C:\> Set-Location C:\Windows

The second command uses the **Push-Location** cmdlet to push the current location (C:\Windows) onto the current location stack and change to the System32 subdirectory. Because no stack is specified, the current location is pushed onto the current location stack. By default, the current location stack is the unnamed default location stack.
C:\WINDOWS> Push-Location System32

The third command uses the *StackName* parameter of the **Push-Location** cmdlet to push the current location (C:\Windows\System32) onto the Stack2 stack and to change the current location to the WindowsPowerShell subirectory. If the Stack2 stack does not exist, **Push-Location** creates it.
C:\Windows\System32> Push-Location WindowsPowerShell -StackName Stack2

The fourth command uses the *Stack* parameter of the **Get-Location** cmdlet to get the locations in the current location stack. By default, the current stack is the unnamed default location stack.
C:\WINDOWS\system32\WindowsPowerShell> Get-Location -Stack
Path
----
C:\WINDOWS

The fifth command uses the *StackName* parameter of the **Get-Location** cmdlet to get the locations in the Stack2 stack.
PS C:\>
C:\WINDOWS\system32\WindowsPowerShell>get-location -stackname Stack2



Path
----
C:\WINDOWS\system32

此命令演示如何使用 Get-LocationStackStackName 参数列出当前位置堆栈和备用位置堆栈中的位置。 有关位置堆栈的详细信息,请参阅说明。

示例 4:自定义 Windows PowerShell 提示符

PS C:\>
function prompt { 'PowerShell: ' + (get-location) + '> '}
PowerShell: C:\WINDOWS>

此示例演示如何自定义 Windows PowerShell 提示符。 定义提示符的函数包括 Get-Location 命令,每当提示符出现在控制台中时,都会运行该命令。

默认 Windows PowerShell 提示符的格式由名为 prompt 的特殊函数定义。 您可以通过创建名为 prompt 的新函数来更改控制台中的提示。

若要查看当前提示函数,请键入以下命令:Get-Content Function:prompt

该命令以 function 关键字开头,后跟函数名称 prompt。 函数体显示在大括号内({})。

此命令定义一个以字符串 PowerShell: 开头的新提示符。 为了追加当前位置,它使用 Get-Location 命令,该命令在调用 prompt 函数时运行。 提示以字符串“>”结尾。

参数

-PSDrive

指定此 cmdlet 在作中获取的指定 Windows PowerShell 驱动器中的当前位置。

例如,如果您在 Certificate: 驱动器中,则可以使用此参数在 C: 驱动器中查找您的当前位置。

参数属性

类型:

String[]

默认值:None
支持通配符:False
不显示:False

参数集

Location
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-PSProvider

指定此 cmdlet 在作中获取的 Windows PowerShell 提供程序支持的驱动器中的当前位置。

如果指定的提供程序支持多个驱动器,则此 cmdlet 返回最近访问的驱动器上的位置。

例如,如果您在 C: 驱动器中,则可以使用此参数在 Windows PowerShellRegistry 提供程序的驱动器中查找您的当前位置。

参数属性

类型:

String[]

默认值:None
支持通配符:False
不显示:False

参数集

Location
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-Stack

指示此 cmdlet 显示当前位置堆栈中的位置。

若要显示其他位置堆栈中的位置,请使用 StackName 参数。 有关位置堆栈的信息,请参阅“说明”。

参数属性

类型:SwitchParameter
默认值:None
支持通配符:False
不显示:False

参数集

Stack
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-StackName

以字符串数组的形式指定命名位置堆栈。 输入一个或多个位置堆栈名称。

若要显示当前位置堆栈中的位置,请使用 Stack 参数。 若要使位置堆栈成为当前位置堆栈,请使用 Set-Location 参数。 有关位置堆栈的信息,请参阅“说明”。

此 cmdlet 无法显示未命名的默认堆栈中的位置,除非它是当前堆栈。

参数属性

类型:

String[]

默认值:None
支持通配符:False
不显示:False

参数集

Stack
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-UseTransaction

在活动事务中包含该命令。 此参数仅在事务正在进行时有效。 有关详细信息,请参阅about_Transactions。

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False
别名:usetx

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

CommonParameters

此 cmdlet 支持通用参数:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 有关详细信息,请参阅 about_CommonParameters

输入

None

不能通过管道将输入传递给此 cmdlet。

输出

System.Management.Automation.PathInfo or System.Management.Automation.PathInfoStack

如果使用 StackStackName 参数,则此 cmdlet 将返回 StackInfo 对象。 否则,它将返回 PathInfo 对象。

备注

  • 此 cmdlet 的设计目的是与任何供应商公开的数据进行协作。 若要列出会话中的提供程序,请键入 Get-PSProvider。 有关详细信息,请参阅about_Providers。

    PSProviderPSDriveStack以及 StackName 参数交互的方式取决于提供程序。 某些组合将导致错误,例如指定驱动器和未公开该驱动器的提供程序。 如果未指定任何参数,此 cmdlet 将返回包含当前工作位置的提供程序的 PathInfo 对象。

    堆栈是最后一个先出列表,其中仅可访问最近添加的项。 按使用项的顺序将项添加到堆栈中,然后检索它们以反向顺序使用。 Windows PowerShell 允许将提供程序位置存储在位置堆栈中。 Windows PowerShell 将创建一个未命名的默认位置堆栈,你可以创建多个命名的位置堆栈。 如果未指定堆栈名称,Windows PowerShell 将使用当前位置堆栈。 默认情况下,未命名的默认位置是当前位置堆栈,但你可以使用 Set-Location cmdlet 更改当前位置堆栈。

    若要管理位置堆栈,请使用 Windows PowerShellLocation cmdlet,如下所示。

    • 若要将位置添加到位置堆栈,请使用 Push-Location cmdlet。

    • 若要从位置堆栈获取位置,请使用 Pop-Location cmdlet。

    • 若要显示当前位置堆栈中的位置,请使用 Get-Location cmdlet 的 Stack 参数。 若要在命名位置堆栈中显示位置,请使用 Get-Location cmdlet 的 StackName 参数。

    • 若要创建新的位置堆栈,请使用 Push-Location cmdlet 的 StackName 参数。 如果指定不存在的堆栈,推送位置 创建堆栈。

    • 若要将位置堆栈设为当前位置堆栈,请使用 Set-Location cmdlet 的 StackName 参数。

    仅当默认位置堆栈是当前位置堆栈时,才完全可访问未命名的默认位置堆栈。 如果将命名位置堆栈设置为当前位置堆栈,则不能再使用 Push-LocationPop-Location 从默认堆栈添加或获取项,也不能使用此 cmdlet 命令显示未命名堆栈中的位置。 若要使未命名堆栈成为当前堆栈,请使用 Set-Location cmdlet 的 StackName 参数,其值为 $null 或空字符串 (“”)。