Move devices into a folder using LastUsed

Move devices into a folder using LastUsed.

Written by Giuseppe

Last published at: November 19th, 2020

Script Events:

  1. discovers if there are any devices in the original folder older than a certain date
  2. if it finds at least one, then it outputs the details into an external grid view window and queries the administrator if he/she wants to move all the discovered devices
  3. if the answer is 'yes' then it moves the devices to another folder
  4. if the answer is 'no' or it hasn't been chosen (then it defaults to 'no'), then the script will exit


$creds = Get-Credential

Connect-TSTMGMTServer -Uri "SERVERuri" -Credentials $creds   #specify server URI

#specify date limit

$date = (get-date).AddDays(-7)


$oldDest = (Get-TSTMGMTFolder -FolderName '\Devices\Folder1').ObjectId      #specify origin folder

$newDest = (Get-TSTMGMTFolder -FolderName '\Devices\Folder2').ObjectId    #specify destination folder

$Devices = Get-TSTMGMTDevices | where {($_.LastUsed -lt $date) -and ($_.FolderId -eq $olddest)}

if ($Devices -ne $null){

Write-Host "Found older than $date !" -ForegroundColor Green

$Devices | Select DeviceName, LastBootTime, LastHeardFrom, LastUser, LastUsed, ProductType | Out-GridView

## Proceed with move or quit

Write-host "Would you like to move the machines (Default is No)" -ForegroundColor Yellow

    $Readhost = Read-Host " ( y / n ) "

    Switch ($ReadHost)


     #if yes:

       Y {

       Write-host "Yes, move." -ForegroundColor Green

       Write-Host "Moving..."  -ForegroundColor Yellow

        $Devices | foreach {

        Move-TSTMGMTObject -ObjectId $_.ObjectId -DestinationFolderId $newdest -ObjectType Device



             #if No:

       N {

       Write-Host "No, skip." -ForegroundColor Green}

       Default {Write-Host "Default, skip." -ForegroundColor Green}



else {

Write-Host "No devices older than $date" -foregroundcolor Green}

Write-Host "Done" -ForegroundColor Cyan