Updated March 24, 2023
Introduction to PowerShell Get-Location
In general many times we forget where we are currently, I mean in which directory we are. Never get confused from Get-location location means your physical address, it is about your directory, where you are working. So if you know a little bit PWD (present working directory) which is used in Linux based system then it’s almost the same. PowerShell Get-location command always gives you the current working directory. Let me tell one important uses of it, suppose you are writing one script which will download any file in the same directory where the script is, then we can use Get-Location function.
Here We will pass the path of download = Get-Location inside the script and we will see all the downloads are happening in the same directory where the script is. Its main benefit is, suppose you shared your script with any other person and he is running your script on his system then he does not require to mention download path manually as Get-Location will automatically find his system current path and download in that directory.
Syntax and Parameters of PowerShell Get-Location
A very simple syntax for PowerShell Get-Location is given below:
Syntax:
Get-Location
[-PSProvider <Array of String]
[-PSDrive <Array of String>]
[<CommonParameters>]
Get-Location [-stack]
[-stackName <Array of String>]- .
[-UseTransaction]
[CommonParameters]
Get-Location always return object. It has certain parameters to perform some extra work, instead of just getting Current working directory names. Let us discuss it’s parameters.
Parameters
Let us discuss PowerShell get-location parameters in detail:
- -PSDrive: Get-Location gives current working directories if we want to get current location in PowerShell specific Drive than we can use -PSDrive command. For example, suppose you are currently inside /home/Ranjan/etc directory and you want to see current location in/home/Ranjan/Document than you can use this command.
- -PSProvider: Now, if you wanted to find the current location of the registry by using PowerShell then you can use -PSProvider, For example, if you are in directory “/home/Ranjan/etc” and you wanted to get location of current PowerShellRegistry provider.
- -stack: You must have heard of stack, Stack is LIFO(last in last out). Here stack entry will directories locations. So suppose you want to see all previous locations then we can use location push command in PowerShell. let me explain to you one example if you are in the directory “/Ranjan” and you push “/file1” then you pushed “/file” to and then you pushed “/file3”. So finally you can get your pushed directories in the file format. I explained it in the example part.
- -stackName: It shows locations in a certain defined path stack. we can create our own path stack. If you want to create your own path stack push location with any stack name. Again you can get back your stack with a name you defined.
- -UseTransaction: These are like auxiliary commands, they are generally at the end of the command. It contains active commands like -ErrorVariable, -WarningAction, -WarningVariable,-ErrorAction,-ErrorVariable,-OutBuffer -OutVariable,-Verbose,-Debug.
Examples to Implement PowerShell Remove-Item
This is the example to implement PowerShell remove-item which are given below:
Example #1
In the below example we are writing a very simple command, which you may need to run many times in your general work. Here, in this command, we are simply getting the current working directory.
As we know in the introduction itself, that Get-Location command is very useful because many times if we write script with static path, and if the same script goes to other systems it will not work, because we have mentioned static path like on my system path was ”/home/Ranjan/Document”, and i wrote static, but if we run the same script on your system it will through error because on your system path may be “/home/yourusername/Document” and “/home/Ranjan/Document” may not recognized.
So writing Get-location will work on every system as the path was dynamic and it will return “/home/Ranjan/Document” on my system and will return “/home/your username/Document” on your system.
Code:
Get-Location
It will return an object. In the example below, we show the same thing as the PWD command for Linux that does the same thing. We do not need to learn pwd Linux command it for those who know it they can make little analysis.
Example #2
Let’s write some more complex Get-Location. We discussed -StackName. So here in this, we can push locations and you can see examples in the given below image. By writing Get-Location -Stack it returns all directories which we pushed. These kinds of situations are useful if you are writing any script and you wanted your script to capture all path and directory and then you can use these things. Remember it’s concept is very similar to the stack of any programing language. As you can push directories and also you can get them back.
Code:
Get-Location -Stack
Push-Location ./ranjan/ -StackName Stack2
Get-Location -StackName Stack2
Output:
Example #3
PowerShell also gives you options to customize the Get-Location command. Customization means getting output according to our requirement or changing output of the Get-location command. Let me tell you one real-time situation. Suppose you are writing any script and your script is giving various outputs according to its input, Now if the same script will be used by other people and he wanted to understand all output he may get confused because if he is getting output like “home/Ranjan/etc”,” home/Ranjan/Document” etc.
So how he will understand that which output is telling about the current directory because all are directory only. So in the below example, we are writing a function which is telling more details or changed message. you can write your own.
Code:
Function customisation {‘Location is: ’ +(get-location) + ‘>’}
customization
Output:
Location is /home/Ranjan/Documents
This output is very much clear to anyone that it is about current directories. You can write something else message according to your needs. Here we are concating string message with a dynamic output of directories.
Example #4
You can write multiple functions like customizations according to your uses.
Code:
Function customisation {‘Current working: ’ +(get-location) + ‘>’}
Customisation
The location is current working: /home/Ranjan/Documents/projects/productms/sitemap
Code:
Function customisation {‘Older directory is : ’ +(get-location) + ‘>’}
Customisation
Older directory is : /home/Ranjan/Documents/projects/productms/sitemap
Output:
Conclusion
It is a daily used command, in general programming, we will face most of the time to get current working directories names. Main uses of PowerShell Get-Location is to make dynamic directories names, instead of manually writing directories names, so that the same script works on other systems also.
Recommended Articles
This is a guide to PowerShell Get-Location. Here we discuss the syntax and parameters of PowerShell Get-Location along with its examples and code implementation. You may also look at the following articles to learn more-