共用方式為


Get-Location

取得目前工作位置或位置堆疊的相關信息。

語法

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 來顯示位置堆疊中的位置。 如需詳細資訊,請參閱 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

此命令示範如何使用 StackStackNameGet-Location 參數來列出目前位置堆棧和替代位置堆疊中的位置。 如需位置堆疊的詳細資訊,請參閱附註。

範例 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

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

例如,如果您位於憑證: 磁碟驅動器中,您可以使用此參數在 C: 磁碟驅動器中尋找目前的位置。

類型:String[]
Position:Named
預設值:None
必要:False
接受管線輸入:True
接受萬用字元:False

-PSProvider

指定此 Cmdlet 在作業中取得的 Windows PowerShell 提供者所支援的磁碟驅動器目前位置。

如果指定的提供者支援一個以上的磁碟驅動器,這個 Cmdlet 會傳回最近存取的磁碟驅動器上的位置。

例如,如果您在 C: 磁碟驅動器中,您可以使用此參數,在 Windows PowerShellRegistry 提供者的磁碟驅動器中找到您目前的位置。

類型:String[]
Position:Named
預設值:None
必要:False
接受管線輸入:True
接受萬用字元:False

-Stack

指出此 Cmdlet 會顯示目前位置堆疊中的位置。

若要在不同的位置堆疊中顯示位置,請使用 StackName 參數。 如需位置堆疊的相關信息,請參閱附註。

類型:SwitchParameter
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-StackName

將具名位置堆疊指定為字串陣列。 輸入一或多個位置堆疊名稱。

若要顯示目前位置堆疊中的位置,請使用 Stack 參數。 若要將位置堆疊設為目前的位置堆疊,請使用 Set-Location 參數。 如需位置堆疊的相關信息,請參閱附註。

此 Cmdlet 無法顯示未命名的預設堆疊中的位置,除非它是目前的堆疊。

類型:String[]
Position:Named
預設值:None
必要:False
接受管線輸入:True
接受萬用字元:False

-UseTransaction

在作用中交易中包含 命令。 只有在交易進行中時,此參數才有效。 如需詳細資訊,請參閱 about_Transactions。

類型:SwitchParameter
別名:usetx
Position:Named
預設值:False
必要:False
接受管線輸入:False
接受萬用字元: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 命令來顯示未命名堆棧中的位置。 若要將未命名的堆疊設為目前的堆疊,請使用 Set-Location Cmdlet 的 StackName 參數,其值為 $null 或空字符串 (“)。