共用方式為


Get-Location

取得目前工作位置或位置堆疊的相關資訊。

Syntax

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

Description

Get-Location Cmdlet 會取得代表目前目錄的物件,就像列印工作目錄 (pwd) 命令一樣。

當您在 Windows PowerShell 磁碟機之間移動時,Windows PowerShell 會保留您在每個磁碟機中的位置。 您可以使用此 Cmdlet 來尋找每個磁碟驅動器中的位置。

您可以使用此 Cmdlet 在執行時間取得目前目錄,並在函式和腳本中使用,例如在函式中,在 Windows PowerShell 提示字元中顯示目前目錄的函式中。

您也可以使用此 Cmdlet 來顯示位置堆疊中的位置。 如需詳細資訊,請參閱 Stack 和 StackName 參數的附註和描述。

範例

範例 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-LocationStack 和 StackName 參數來列出目前位置堆疊和替代位置堆疊中的位置。 如需位置堆疊的詳細資訊,請參閱附註。

範例 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 關鍵詞,後面接著函式名稱,提示字元。 函式主體會出現在大括弧內 ( {} ) 。

此命令會定義以字串 PowerShell 開頭的新提示:。 若要附加目前的位置,它會使用 Get-Location 命令,此命令會在呼叫提示函式時執行。 命令提示字元的結尾為 "> " 字串。

參數

-PSDrive

指定指定 Windows PowerShell 磁碟驅動器中這個 Cmdlet 在作業中取得的目前位置。

例如,如果您是在憑證: 磁碟驅動器中,您可以使用此參數來尋找 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

如果您使用 Stack 或 StackName 參數,此 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 參數。 如果您指定的堆疊不存在,Push-Location 會建立該堆疊。

    • 若要將位置堆疊設為目前的位置堆疊,請使用Set-Location Cmdlet 的 StackName 參數。

    未命名的預設位置堆疊只有在做為目前的位置堆疊時,才能供完整存取。 如果您將具名位置堆疊設為目前的位置堆疊,就無法再使用 Push-LocationPop-Location 新增或從預設堆疊取得專案,或使用這個 Cmdlet 命令來顯示未命名堆疊中的位置。 若要將未命名的堆疊設為目前的堆疊,請使用Set-Location Cmdlet 的 StackName 參數,其值為 $null 或空字串 (“”“) 。