Octopus Deploy Documentation

Update variable set variable value

Last updated

This script demonstrates how to programmatically update a matching variable value stored in a library variable set.

Note: This script does not alter the variable scopes, only the value.


Provide values for the following:

  • Octopus URL
  • Octopus API Key
  • Name of the space to search
  • Name of the library variable set to use
  • Variable name to search for
  • New variable value to replace existing value


$ErrorActionPreference = "Stop";
# Define working variables
$octopusURL = "http://your.octopus.app/"
$octopusAPIKey = "YOUR-APIKEY"
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }

# Specify the Space to search in
$spaceName = ""

# Library Variable Set
$libraryVariableSetName = ""

# Variable name to search for
$VariableName = ""

# New variable value to set
$VariableValue = ""

$space = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Headers $header) | Where-Object {$_.Name -eq $spaceName}

Write-Host "Looking for library variable set '$libraryVariableSet'"
$LibraryvariableSets = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/libraryvariablesets?contentType=Variables" -Headers $header)
$LibraryVariableSet = $LibraryVariableSets.Items | Where-Object { $_.Name -eq $libraryVariableSetName }

if ($null -eq $libraryVariableSet) {
    Write-Warning "Library variable set not found with name '$libraryVariableSetName'."

$LibraryVariableSetVariables = (Invoke-RestMethod -Method Get -Uri "$OctopusURL/api/$($Space.Id)/variables/$($LibraryVariableSet.VariableSetId)" -Headers $Header) 

for($i=0; $i -lt $LibraryVariableSetVariables.Variables.Length; $i++) {
    $existingVariable = $LibraryVariableSetVariables.Variables[$i];
    if($existingVariable.Name -eq $VariableName) {
        Write-Host "Found existing variable, updating its value"
        $existingVariable.Value = $VariableValue

$existingVariable = $LibraryVariableSetVariables.Variables  | Where-Object {$_.name -eq $VariableName} | Select-Object -First 1 

$UpdatedLibraryVariableSet = Invoke-RestMethod -Method Put -Uri "$OctopusURL/api/$($Space.Id)/variables/$($LibraryVariableSetVariables.Id)" -Headers $Header -Body ($LibraryVariableSetVariables | ConvertTo-Json -Depth 10)   

Need support? We're here to help.