EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Get-Location
Secondary Sidebar
PowerShell Tutorial
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell Get-Location

By Priya PedamkarPriya Pedamkar

PowerShell-Get-Location

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:

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,241 ratings)

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Get-Location
[-PSProvider <Array of String] [-PSDrive <Array of String>] [<CommonParameters>] Get-Location [-stack] [-stackName <Array of String>]- .
[-UseTransaction] [CommonParameters]

Note: stackName (here we can write the name of the stack like stack1 stack2 by pushing location and also we can get back the same name)

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

PowerShell Get-Location eg1

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.

PowerShell Get-Location eg2

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:

PowerShell Get-Location eg3

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:

eg3.1

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:

 eg4

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-

  1. PowerShell ForEach Loop
  2. PowerShell Variables
  3. PowerShell String Functions
  4. PowerShell Operators
Popular Course in this category
All in One Data Science Bundle (360+ Courses, 50+ projects)
  360+ Online Courses |  1500+ Hours |  Verifiable Certificates |  Lifetime Access
4.7
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
Data Visualization Training (15 Courses, 5+ Projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more