Skip to main content
Question

Remove Pinned Devices From Inactive Accounts

  • March 11, 2026
  • 0 replies
  • 7 views

jorowi
Forum|alt.badge.img

I would like to remove pinned devices in our enterprise on accounts that have had no activity in 90 days. I wrote the following with the help of Gemini and it appears it will work. I’m new to the Box CLI so if anyone else with more experience can take a look at this I’d appreciate it.

 

# 1. Calculate the date 90 days ago in ISO 8601 format
$thresholdDate = (Get-Date).AddDays(-90)
Write-Host "Looking for users inactive since: $($thresholdDate.ToShortDateString())" -ForegroundColor Cyan

# 2. Get all users and filter by 'modified_at'
# Note: --max-items 5000 ensures we don't just get the first page
$inactiveUsers = box users --all-users --json --max-items 5000 | ConvertFrom-Json | Where-Object {
[datetime]$_.modified_at -lt $thresholdDate
}

if ($inactiveUsers.Count -eq 0) {
Write-Host "No inactive users found. Exiting." -ForegroundColor Green
exit
}

Write-Host "Found $($inactiveUsers.Count) inactive users. Fetching device pins..." -ForegroundColor Yellow

# 3. Get all enterprise device pins
$allPins = box device-pins --json --max-items 5000 | ConvertFrom-Json

# 4. Loop through inactive users and delete their pins
foreach ($user in $inactiveUsers) {
# Find pins belonging to this specific user
$userPins = $allPins | Where-Object { $_.owned_by.id -eq $user.id }

foreach ($pin in $userPins) {
Write-Host "Removing pin $($pin.id) ($($pin.product_name)) for user: $($user.name)" -ForegroundColor Magenta

# UNCOMMENT THE LINE BELOW TO ACTUALLY DELETE
# box device-pins:delete $($pin.id) --yes
}
}

Write-Host "Process complete." -ForegroundColor Green