user2280319
user2280319

Reputation: 15

How can I use an Environmental Variable as a start of a directory location in powershell?

I was given a powershell script by a coworker to try and figure out. I have very little expereince with it so I've gotten stuck.

We need to pull a user defined cmdlet from a .ps1 file form another part of the drive.

Normally you would do it something like this:

    . .\scripts\thing.ps1

But we want to use an environmental variable set in command prompt to start the location. We have something like:

    . $Env:JobDir\scripts\thing.ps1

But this returns the error

    . : The term '\scripts\thing.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. 

Is there anyway to make something like this work?

Upvotes: 0

Views: 47

Answers (2)

David Martin
David Martin

Reputation: 12248

You can do something like this:

# build the path to the job  
$job = Join-path -Path $Env:JobDir -ChildPath "scripts\thing.ps1"

# execute the job
$job 

or

.$("$Env:JobDir\scripts\thing.ps1")

Upvotes: 1

Tim Ferrill
Tim Ferrill

Reputation: 1684

You'd need to make it a string, otherwise it's interpreting the entire path as a variable.

"$Env:JobDir\scripts\thing.ps1"

or

$Env:JobDir + "\scripts\thing.ps1"

Upvotes: 3

Related Questions