PowerShell - Move devices into a folder using LastUsed
Move devices into a folder using LastUsed.
Script Events:
- discovers if there are any devices in the original folder older than a certain date
- 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
- if the answer is 'yes' then it moves the devices to another folder
- if the answer is 'no' or it hasn't been chosen (then it defaults to 'no'), then the script will exit
Script:
$creds = Get-Credential
Connect-TSTMGMTServer -Uri "SERVERuri" -Credentials $creds #specify server URI
#specify date limit
$date = (get-date).AddDays(-7)
#params
$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