PowerShell PowerTip: Grabbing the location your script lives in

Kory Thacher

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!


Discussion is closed.

Feedback usabilla icon