Nerd in Training
Nerd in Training

Reputation: 2220

How to get parent folder of a directory

I'm currently writing a script (for the first time) in powershell that gets called by tfs while building. What I have are the environment variable provided to me such as:

$Env:TF_BUILD_BINARIESDIRECTORY

What I need is to get the parent to this directory. How do I do this?

Update: Really, I'm just trying to do a Copy-Item to the parent of the parent of $Env:TF_BUILD_BINARIESDIRECTORY

Upvotes: 1

Views: 5215

Answers (3)

Ben Thul
Ben Thul

Reputation: 32687

You're thinking too hard.

cp $yourFile $Env:TF_BUILD_BINARIESDIRECTORY\..\..

Happy scripting!

Upvotes: 2

Viet Trang
Viet Trang

Reputation: 21

For a string containing the parent's parent, try this:

(get-item $Env).parent.parent.fullname

Upvotes: 2

Knuckle-Dragger
Knuckle-Dragger

Reputation: 7046

From inside the .ps1 script you can use the MyInvocation variable and split path to the parent. (assuming your .ps1 is in the TF_BUILD_FOLDER already)

Split-Path -Parent $myInvocation.MyCommand.Definition

or this might work.

Split-Path -Parent $Env:TF_BUILD_BINARIESDIRECTORY

Upvotes: 1

Related Questions