Get-Location

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

语法

Get-Location
   [-PSProvider <String[]>]
   [-PSDrive <String[]>]
   [-UseTransaction]
   [<CommonParameters>]
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

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

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

参数

-PSDrive

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

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

Type:String[]
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-PSProvider

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

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

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

Type:String[]
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Stack

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

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

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-StackName

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

若要显示当前位置堆栈中的位置,请使用 Stack 参数。 若要将某个位置堆栈设为当前位置堆栈,请使用 Set-Location 参数。 有关位置堆栈的信息,请参阅“备注”。

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

Type:String[]
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-UseTransaction

在活动事务中使用该命令。 仅当正在执行事务时,此参数才有效。 有关详细信息,请参阅 about_Transactions。

Type:SwitchParameter
Aliases:usetx
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

输入

None

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

输出

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

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

备注

  • 此 cmdlet 用于处理由任何提供程序公开的数据。 若要列出会话中的提供程序,请键入 Get-PSProvider。 有关详细信息,请参阅 about_Providers。

    PSProviderPSDriveStackStackName 参数的交互方式取决于提供程序。 某些组合将会导致错误,例如,指定驱动器以及没有公开该驱动器的提供程序。 如果未指定参数,此 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 参数。 如果指定不存在的堆栈,则 Push-Location 创建该堆栈。

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

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