Scripting tips – Point to the dir from which the script was run.

Standard

When working with Powershell you often need to interact with other files when executing your script. Dealing with input files, output files and temp files are usually integral parts of script writing. When working with files you usually need to specify the absolute path to each file. However this makes the script less “robust” as you will need to change these paths if you also change the location of the script by copying it or moving it. This means extra work as you will need to modify the script every time you move it or copy it. Furthermore if you intend on giving the script for someone else to run they might complain that it’s not working because they haven’t got the paths right.

– Creating an object which always points to the script dir.

I have already used the following lines of code in many of my scripts but I find them so useful I thought they deserved their own post.

$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath

The whole thing is as simple as writing the above two lines of code. They will create the object $dir which always point to the path from where this script was run.

– How do I use this when referring to files in my scripts?

Now, when you wish to add a file to your script you can do it like the examples shown below.

$inputFile = "$dir\input.txt"

$outputFile = "$dir\output.txt"
New-Item -ItemType File $outputFile -Force

$logFile = "$dir\logs\logfile.txt"
New-Item -ItemType File $logFile -Force

$inputFile now points to a file called “input.txt” located in your script directory.

The $dir object is also useful when creating new files to make sure they are created in the correct locations. For instance $outputFile is a path that points to a file “output.txt” in the script directory. If it doesn’t exist yet you can then create the file with this line: New-Item -ItemType File $outputFile -Force.

Also, if you for instance want to add logging functionality to your script you can create the log file in a new dir in your script directory called “logs” by using the last example:
$logFile = “$dir\logs\logfile.txt”
New-Item -ItemType File $logFile -Force

– Benefits of using file paths like this

As you can see using the $dir object also allows you to easily maintain a directory structure for your files which will always remain the same no matter where you move the script. If you now create a script and refer to your files/directories like above and give it to a friend/colleague, it will always work no matter where he runs it from and any script output files will be created in the script directory. This will make the script much easier for others to use and save you a lot of trouble.

– Finding the correct scope when using MyInvocation.MyCommand.Path

In most cases creating the $dir object like above will work perfectly without any problems. However in some cases you will need to work a little with the scope of the variable in order to make it work. For instance if you are using it in a child job or from a .NET form you are running you might need to change the code to look like this:

$scriptPath = $script:MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath

If you run into any trouble you can change to code to look like above which should solve the problems.
You can read more about scopes in powershell here:
http://technet.microsoft.com/en-us/library/hh847849.aspx

– That’s it!

I hope this post will be helpful to you and let you easily work with paths and files in your scripts.

Advertisements