PowerShell - Delete Devices using LastUsed

Delete Devices using LastUsed.

Written by Giuseppe

Last published at: January 27th, 2023

This cmdlet will delete devices if the LastUsed is less than X days.

Import-Module ThinScaleManagement
$creds = Get-Credential

Connect-TSTMGMTServer -uri "http://server/tstmgmt" -Credentials $creds

$date = (get-date).AddDays(-30)      #Set date limit
$DeviceID = Get-TSTMGMTDevices | where {$_.LastUsed -lt $date} | select DeviceName, LastUsed, ObjectId    #discover devices older than set date
$DeviceID | Out-GridView

Write-Host "Discovering Date and Device..." -ForegroundColor Green

$allData = Get-TSTMGMTAllData

foreach ($device in $allData.Devices | Where-Object {$_.LastUsed -lt $date })
    $confirmation = Read-Host "Are you Sure You Want To Delete The Device " $device.DeviceName "(ProductVersion: " $device.ProductVersion ", LastUsed: " $device.LastUsed " )"
    if ($confirmation -eq 'y') {
      Remove-TSTMGMTObject -objectId $device.ObjectId -objectType 'Device'
    write-Host "No, skip removal." -ForegroundColor Yellow