Powershell: Get Execution Path of Script

By June 1, 2012PowerShell

I have been writing a script which generated a report. I had used Out-File to write to a file and this had been working as expected for some time. Today however, I opened up PowerShell and entered the path of the script without changing directory to the script location first. This caused the report file to be written to a directory other than the script itself. I quite like having the report and script in the same directory as it keeps it all together. So I thought, how do I insure the script writes the report file where it’s being run from. The solution is actually pretty simple…

As you can see the sollution is very simple but effective. I’ll be using it future PowerShell scripts I write so thought I’d write an article on the subject incase it helps someone out.

Chris Colden

Chris Colden

28 | IT Consultant | Web Dev'er | Geek | Metal Head | Gamer | Tea, Beer and Wine Drinker | Slightly Wonky | All views my own and not of my employer(s)

Leave a Reply