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

A newbie mistake with PowerShell

At work, I wrote a PowerShell script to automate our build process. I had to use the Visual SourceSafe command line tool to check out the files, make changes, and check them back in. The SS tool expected the ssdir environment variable to be set properly. I had just begun using PowerShell and I made the foolish mistake of assuming that the Set-Variable command could set environment variables. It took me a few minutes to realize this folly 😳

For other PowerShell newbies, the following snippet should make things clear :-

Set-Variable ssdir \\somemachine\vss
((Get-Variable ssdir).Value -eq $env:ssdir)

The output of line 3 will be False.