tags:

views:

33

answers:

2

Hi All, I have written a powershell script. the code has paths related to only my PC. Now the same code cannot be executed by another person on his machine because the path is diff. Therefore please let me know a way where my code can work on all machines.

+1  A: 

It depends on the paths. If they're to programs in \Program Files perhaps you can use the environment variable $env:ProgramFiles in your path spec. You can also parameterize your script to take the path like so:

param($path)
# rest of script ...

Note that the param() statement must be the first non-comment line in your script.

Keith Hill
What if I have to execute the script in a production server? Prod Server will have a diff path... therfore I needed a soln. I ll try your advise, Thanks
A: 

You could also use the special $MyInvocation variable available to running scripts. It has access to the path the script was executed from, among other things.

For example a script I use has this line:

$InputCSV = (split-path $myinvocation.mycommand.path) + "\filename.csv"

Which means no matter where the script is run from it will know to grab the CSV file from the same place.

EdgeVB