“Did not call you, because you where away in Teams mostly the complete day!”, something I heard sometimes. And I was wondering, because I was “active”, I was working in other applications.
What I found: If you minimize the new Microsoft Teams Client, it switches to “away” after a while. Even if you are “Active”. Even if you use tools like “Awake” (Part of PowerToys) or “Caffeine”
So, I came up with a small PowerShell function to solve this:
function Invoke-KeepYouGreenInTheNewMicrosoftTeams
{
<#
.SYNOPSIS
Keep you "green" in Teams, while the process is in the background
.DESCRIPTION
Keep you "green" in Teams, while the process is in the background
.PARAMETER LoopTime
Time, in seconds, to wait between the loops.
The default is 120 (e.g., 2 minutes)
Minimum is 30 seconds (Do not brute force Teams!)
Maximum is 300 seconds (Should prevent status flickering)
.EXAMPLE
PS C:\> Invoke-KeepYouGreenInTheNewMicrosoftTeams
Keep you "green" in Teams, while the process is in the background
.NOTES
"Did not call you, because you where away in Teams!"
And I was active, but Microsoft Teams was minimized (Background) and I did my job: Write Code in my favorite editor.
So, I came up with this Idea!
What doe it so?
It activates the minimized Microsoft Teams app, send some dummy key strokes and make a inactive again.
You current window should not even realize that we did that!
And as a benefit, it should keepalive your session, like "awake" (PowerToys) or "Caffeine"!
Please do not abuse this in any kind...
#>
[CmdletBinding(ConfirmImpact = 'None')]
[OutputType([string])]
param
(
[Parameter(ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[ValidateRange(30, 300)]
[ValidateNotNullOrEmpty()]
[Alias('TimeToWait')]
[int]
$LoopTime = 120
)
begin
{
$null = (Add-Type -AssemblyName UIAutomationTypes -ErrorAction SilentlyContinue)
$null = (Add-Type -AssemblyName UIAutomationClient -ErrorAction SilentlyContinue)
# https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow?redirectedfrom=MSDN
$Win32ShowWindowAsync = (Add-Type -MemberDefinition @'
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
'@ -Name 'Win32ShowWindowAsync' -Namespace Win32Functions -PassThru)
# For the window states, from above
$WindowStates = @{
'FORCEMINIMIZE' = 11
'HIDE' = 0
'MAXIMIZE' = 3
'MINIMIZE' = 6
'RESTORE' = 9
'SHOW' = 5
'SHOWDEFAULT' = 10
'SHOWMAXIMIZED' = 3
'SHOWMINIMIZED' = 2
'SHOWMINNOACTIVE' = 7
'SHOWNA' = 8
'SHOWNOACTIVATE' = 4
'SHOWNORMAL' = 1
}
# This should never happen, right?
if ($null -eq $LoopTime)
{
[int]$LoopTime = 120
}
}
process
{
while ($true)
{
$TeamsApp = $null
$TeamsApp = (Get-Process -Name 'ms-teams' -ErrorAction SilentlyContinue)
if ($null -ne $TeamsApp)
{
# The title changes, it contains where you are and what you did, and therefore it can be very long
$TeamsWindowTitle = $null
$TeamsWindowTitle = ($TeamsApp).MainWindowTitle
if ($null -ne $TeamsWindowTitle)
{
# if Minimized. Looks crappy, but it works very well here
if ((([Windows.Automation.AutomationElement]::FromHandle($TeamsApp.MainWindowHandle)).GetCurrentPattern([Windows.Automation.WindowPatternIdentifiers]::Pattern)).Current.WindowVisualState -eq 'Minimized')
{
Write-Verbose -Message 'Keeping you active (green) in Microsoft Teams!'
# Let us creates a new Component Object Model (COM) object, we need this this to send (inject) the keystrokes
# If Windows Script Host (WSH) is disabled, this might fail
$wshell = $null
$wshell = (New-Object -ComObject wscript.shell -ErrorAction SilentlyContinue)
# Make it active, but we keep it in the background (This is the secret sauce here)
$null = ($Win32ShowWindowAsync::ShowWindowAsync($TeamsApp.MainWindowHandle, $WindowStates['SHOWMINIMIZED']))
# Calm down and wait a very short moment here
$null = (Start-Sleep -Milliseconds 500)
# Send the dummy keystrokes, one that Teams don't know, therefore: So nothing
if ($null -ne $wshell)
{
$null = ($wshell.SendKeys('+{F15}'))
}
else
{
Write-Warning -Message 'We cannot send the dummy keystrokes!'
}
# Minimize and inactivate it again
$null = ($Win32ShowWindowAsync::ShowWindowAsync($TeamsApp.MainWindowHandle, $WindowStates['SHOWMINNOACTIVE']))
}
else
{
# Your Teams Client in not in the background! No further action needed here.
Write-Verbose -Message 'Looks like Microsoft Teams is not in the background, so we do nothing here.'
}
}
# Now we wait for the next loop
$null = (Start-Sleep -Seconds $LoopTime)
}
else
{
Write-Verbose -Message 'Looks like Microsoft Teams is not even running.'
# Therefore, we are done here!
break
}
}
}
}
#region LICENSE
<#
BSD 3-Clause License
Copyright (c) 2024, Joerg Hochwald
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#>
#endregion LICENSE
#region DISCLAIMER
<#
DISCLAIMER:
- Use at your own risk, etc.
- This is open-source software, if you find an issue try to fix it yourself. There is no support and/or warranty in any kind
- This is a third-party Software
- The developer of this Software is NOT sponsored by or affiliated with Microsoft Corp (MSFT) or any of its subsidiaries in any way
- The Software is not supported by Microsoft Corp (MSFT)
- By using the Software, you agree to the License, Terms, and any Conditions declared and described above
- If you disagree with any of the terms, and any conditions declared: Just delete it and build your own solution
#>
#endregion DISCLAIMER
What this function does:
- Checks if Microsoft Teams is running
- Checks if Microsoft Teams is minimized
- Send a dummy key stroke (In this case Shift+F15)
- Keeps Microsoft Teams running, minimized and in the background
- It waits and will then run the same procedure again
The function is also published to my main GitHub repository. Any future updates might only be published there.
Feel free to contribute on GitHub!