How do you add bamboo variable in powershell?
Asked by Mihai Sava:

Hello All,

I'm having hard times to figure this out, I have the following powershell script , simple one:

```
$BambooPath = 'C:\StartFolder'$RemoteWebPath = 'C:\DestFolder'Copy-Item -Path $BambooPath -Destination $RemoteWebPath -Recurse
```

I wan't to replace in $BambooPath the C:\\StartFolder with the bamboo.working.directory variable...anyone has an ideea how to do that?


Answers:
author: fabcrm
body: **How to use Bamboo Variables in an EXTERNAL file powershell script.**

This works when your powershell script is file based. You are calling your script using a Script Task.
> Interpreter = Windows Powershell  
> Script location = File  
> Script file = \[full path and filename of your powershell script\]

**From within your file based powershell script:**

```
# notice the use of quotes here if you want to use the .replace extension method.
$mybranch = "$Env:bamboo_planRepository_branch".replace("/","-")

$buildNumber = $Env:bamboo_buildNumber

$myCustomVariable1 = $Env:bamboo_myCustomVariable1
```

The main difference between INLINE and EXTERNAL file base powershell scripts is the way you access the variable. In this example, simply prefix the bamboo variable with $Env: and replace the dots with underscores.

I hope this simple clarification with examples will help others stumbling over this question in the future.


author: fabcrm
body: **How to use Bamboo Variables in an INLINE powershell script.**

Critically, this ONLY works if your powershell script is INLINE. If your powershell script is in a file somewhere, then this will not work

**Examples for inline use of bamboo variables in a powershell script:**

```
$mybranch = "${bamboo.planRepository.branch}".replace("/","-")

$buildNumber = "${bamboo.buildNumber}"

$myCustomVariable1 = "${bamboo.myCustomVariable1}"
```

**Whats going on:**

$mybranch is a local variable, and is assigning the value of bamboo.planRepository.branch variable. In this example, we are replacing the 'forward slashes' with dashes. I thought that might help demonstrate use of the variables too.


author: Thijs Kuperus
body: One solution we found rather easy is to use the variables as arguments.

In your case you should use

```
$BambooPath = $args[0];?

```

And enter as argument : ${bamboo.build.working.directory}

comments: author: Jacek Szybisz
body: This is not very helpful. Maybe you can provide more detailed explanation how you can set it. I am trying to set some build variable from PS script


author: Thijs Kuperus
body: Hi Jacek,

We use a script that needs to know in which state it's used (build or deploy) and another parameter. In the Argument we have:

```
build ${bamboo.repository.git.branch}
```

*Note that the parameters are split with a space.*

For getting the parameters in your Powershell code use this.

```
$state= $args[0]; //returns "build"
$branch = $args[1]; //returns something like "master" or "feature/feature-name"

//Followed by your own code
```


author: Timothy
body: > For task configuration fields, use the syntax ${bamboo.myvariablename}. For inline scripts, variables are exposed as shell environment variables which can be accessed using the syntax $bamboo_MY_VARIABLE_NAME (Linux/Mac OS X) or %BAMBOO_MY_VARIABLE_NAME% (Windows).

comments: author: Mihai Sava
body: If it were that easy...It's not working. ![sad](/html/@C495B4504065515364EB1894F812B34C/images/emoticons/sad.png "sad")