views:

2387

answers:

2

I would like to include script files with such pseudo syntax:

Include '.\scripA.ps1'

But the only thing I have found is some thing like this:

$thisScript = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
. ($thisScript + '.\scriptA.ps1')

that is ugly.

Is there some nice way to include scripts with relative paths?

+3  A: 

Unfortunately no, there is no good way. PowerShell doesn't really support this idea very well at all in V1. Really the approach you are taking is the best approach

JaredPar
And what is about v2? Should I use Modules for external scripts and "Import-Module ./foo.psm1" syntax? Or any improvements for usual scripts inclusion?
alex2k8
@alex2k8, i haven't done much work at all with V2 yet so I can't give too much help. I know that modules are supposed to be much more isolated than scripts but other than that I don't have much data.
JaredPar
In v2, you can use a module with a manifest that defines all modules, scripts, and even snapins/assemblies. A nice benefit of this is that you can have functions and variables that are private to the module.
JasonMArcher
+7  A: 

You can dot-source (include) the file:

. .\scriptA.ps1

To get the full path of the script:

Resolve-Path .\scriptA.ps1

Shay Levy
No, this works until the main script location equals to pwd.
alex2k8
The first part does... but Resolve-Path will check the current directory and your path for the file. If your script directory is in your path ($env:path), it will find it.
Steven Murawski
Thnaks guys, I should have mention the cureent location.
Shay Levy