Keep Teams Icon Green using PowerShell
24 January, 2020
Need to step away from your computer for a bit?
But the problem is, you also need to stay active on Microsoft teams…Don’t worry, PowerShell can help!
In the below video I demonstrate how to use PowerShell to move the mouse and mimic clicks, which will keep your computer active and in turn keep your Microsoft teams icon green. The PowerShell script itself adds a type definition that contains C# code. Its a custom class that uses low-level windows api functions to create mouse clicks.
PowerShell Script:
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class MouseInput {
[StructLayout(LayoutKind.Sequential)]
public struct INPUT {
public int type;
public MOUSEINPUT mi;
}
[StructLayout(LayoutKind.Sequential)]
public struct MOUSEINPUT {
public int dx;
public int dy;
public int mouseData;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
[DllImport("user32.dll", SetLastError = true)]
public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
public const int INPUT_MOUSE = 0;
public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
public const int MOUSEEVENTF_LEFTUP = 0x0004;
public static void Click() {
INPUT[] inputs = new INPUT[2];
inputs[0].type = INPUT_MOUSE;
inputs[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
inputs[1].type = INPUT_MOUSE;
inputs[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
}
}
"@
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
while ($true) {
$pos = [System.Windows.Forms.Cursor]::Position
$newPos = New-Object System.Drawing.Point ($pos.X + 447), ($pos.Y + 1)
[System.Windows.Forms.Cursor]::Position = $newPos
# Perform left click
[MouseInput]::Click()
Start-Sleep -Seconds 3
[System.Windows.Forms.Cursor]::Position = $pos
# Perform left click
[MouseInput]::Click()
Start-Sleep -Seconds 3
}