TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PowerShell Get-Location

PowerShell Get-Location with What is PowerShell, History of PowerShell, Features of PowerShell, PowerShell vs CMD, PowerShell Scripting, PowerShell Versions, PowerShell Commands, PowerShell Looping, PowerShell Conditions, PowerShell Array, PowerShell Brackets etc.

<< Back to POWERSHELL

PowerShell Get-Location

The Get-Location cmdlet gets the information about the current working directory or a location stack.

This cmdlet gets an object which represents the current directory, much like the print present working directory (pwd) command. When we switch between the drives, PowerShell retains our location in each drive. We can also use this cmdlet to find the location in each drive.

This cmdlet is used to get the current directory at execution time and use it in scripts and functions, such as in functions that display the current working directory in the PowerShell prompt. We can also use Get-Location cmdlet to view the location in the location stack.

Syntax

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

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

Parameters

-PSDrive

The -PSDrive parameter is used to specify the current location in the given PowerShell drive that this cmdlet gets in the operation.

For instance, if any user in a Certificate: drive, using this parameter, then he/she can find its current location in the C: drive.

-PSProvider

The -PSProvider parameter is used to specify the current location in a drive supported by the PowerShell provider that this cmdlet gets in the operation. If the specified PowerShell provider supports more than one drive, then Get-Location cmdlet returns a location on the most recently accessed drive.

For example, if any user in C: drive using this parameter, then he/she can find its current location in the drives of PowerShellRegistry provider.

-Stack

The -Stack parameter is used to display the locations in the current location stack. If a user wants to display the locations in the different location stack, he can use the StackName parameter.

-StackName

The -StackName parameter is used to display the locations in the different location stacks.

Get-Location cmdlet cannot display the locations in default stack unless it is the current stack.

Examples

Example1: To display a current drive location

PowerShell Get-Location

The cmdlet in this example displays a location in the current PowerShell drive. Suppose a user is in the Windows directory of D: drive, this cmdlet displays the path to that directory.< /p>

Example2: To display a current location for different drives:

This example explains the use of get-location to display the current location in different PowerShell drives.

i. The first cmdlet in this example uses the set-location cmdlet to set the current location to the Windows directory of C:\ drive.

PowerShell Get-Location

ii. The second cmdlet in this example uses the set-location cmdlet to change the location to the HKLM:\ Software \ Microsoft registry key. When we change a location in the HKLM: drive, Windows PowerShell retains our location in C:\drive.

PowerShell Get-Location

iii. The third cmdlet in this example uses the set-location cmdlet to change a location to the HKCU:\Control Panel\Input method registry key.

PowerShell Get-Location

iv. The fourth cmdlet in this example uses the Get-location cmdlet to find the current location on a C:\ drive. This cmdlet uses the -PSDrive parameter to specify the drive.

PowerShell Get-Location

v. The fifth cmdlet in this example uses the Set-location cmdlet to return to the C:\drive.

PowerShell Get-Location

vi. The sixth cmdlet in this example uses the Get-Location cmdlet to find a current location in the drives supported by the PowerShell registry provider. In this, Get-location returns a location of recently accessed registry drive, HKCU.

PowerShell Get-Location

vii. The seventh cmdlet in this example uses the -PSDrive parameter to see the current location in HKLM: drive.

PowerShell Get-Location

Example3: To customize the PowerShell prompt

PowerShell Get-Location

This example displays how to customize the Windows PowerShell prompt. A function that defines the prompt includes a Get-location cmdlet, which is executed whenever a prompt appears in the console.

This cmdlet begins with the Function keyword followed by the name of a function, which is prompt. The body of a function appears in the braces '{ }'.

This cmdlet defines a new prompt that begins with the 'PowerShell:' string. To add the current location, it uses a get-location cmdlet, which executes when the prompt function is called. The prompt function ends with the string '>'.

Type this command, Get-Content Function:prompt to see the current prompt function.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf