Skip to main content
Question

Put Box CLI result into Powershell variable

  • May 22, 2025
  • 2 replies
  • 27 views

Forum|alt.badge.img

Running a Powershell script with some Box CLI commands in it.  The commands complete successfully and the result is shown on screen/in the Powershell console, but I can't seem to get that result into a variable so the script can determine success/failure and continue processing accordingly.

Specifically, I'm working with the command: Box collaborations:delete

I've tried:

$X = box collaborations:delete $CollabID --as-user $UserID --json

box collaborations:delete $CollabID --as-user $UserID --json > $X

$X = "$(box collaborations:delete $CollabID --as-user $UserID --json)" | ConvertFrom-Json

In all 3 cases $X is null/empty, but "Collaboration <CollabID> successfully removed" is shown on the screen in the Powershell console.

How do I get the "Collaboration <CollabID> successfully removed" in a variable?

2 replies

Forum|alt.badge.img

Hey Scott! 

Thanks for the question. 

This is happening because Box is returning a 204. There are a couple things you can do! 

If you want to see the 204 response code - you can use this syntax in a powershell script vs calling the CLI within a powershell script. 

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer <token>")
try {
$response = Invoke-RestMethod 'https://api.box.com/2.0/collaborations/<collab_id>' -Method 'DELETE' -Headers $headers
} catch {
# Dig into the exception to get the Response details.
# Note that value__ is not a typo.
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
}

If you want to use the CLI in the command still, you could do something like this:

$id="<collab_id>"
$collabResponse = "$(box collaborations:delete $id 2>&1)" | Tee-Object -Variable collabResponse
Write-Host "Response: $collabResponse"

Thanks, 

Alex, Box Developer Advocate


Forum|alt.badge.img

That's perfect!  Just what I needed to know.  Thank you!