Another newbie mistake with PowerShell

When you run an executable from the command line or from a script, the current directory will be the directory from which the executable or script is invoked. This behavior is consistent in both the old command prompt as well as in PowerShell. There is an exception though when you invoke a script, as I found out when I attempted to run a script that opened a text file (to make some changes). I used Push-Location to change to a specific directory, and then invoked the script. The script used a path relative to this directory I had changed to, to open the file. I was surprised to find that it kept failing. That’s when I realized that for the script the current directory would be what’s set in the environment. The right way to do this is to properly set the current directory to what you want it to be. In my case, since I was invoking the script from a specific folder, I just added this line to the script. That fixed the problem.

[Environment]::CurrentDirectory = (Get-Location).Path
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s