June 12th, 2018

PowerShell PowerTip: Grabbing the location your script lives in

Kory Thacher
Premier Field Engineer

One super common thing in PowerShell is to design a script to consume particular files for data. Often times you are building the script right in the folder for your files. However, it can be annoying to give long, literal file paths to a file. Also, if you give that script to someone else, they need to edit that path to point at the right location– how annoying!

There is a built in variable to help with this scenario: $PSScriptRoot will provide the file path the script was run from, which can let you relatively point at files.

You can see an example of this in an older blog post. I received a few questions about what I was doing there so I added this to my list of tips.

If you have any other small annoyances you struggle with, let me know and I’ll see if there is a handy tip to make it simpler.

Hope that helps, tune in more often to get short and sweet PowerTips!

Author

Kory Thacher
Premier Field Engineer

I've been a PFE since 2012, working with various technologies. I live in the modern applications domain, doing work in UWP apps, .NET, Unity, DevOps, and of course PowerShell. I've been teaching PowerShell related workshops very frequently for years, and I really enjoy getting the opportunity to explain a topic or learn something new from my colleagues. I enjoy scripting because of how fast and interactive it can be, and I love getting interesting problems to work on with customers. ...

More about author

0 comments

Discussion are closed.