Automate the sign-out of Microsoft 365 (Word, Excel, Powerpoint, Outlook, etc) using powerhell

Prabhjot Singh 255 Reputation points
2024-08-09T09:05:16.2766667+00:00

Hi Team,

I hope you are doing well. I got a blocker in my script, it is a custom script for signing out from Microsoft 365 application (Word, Excel, Powerpoint, Access, Outlook, Publisher, etc.). In this script, we are using the send-keys commands for automating the sign-out process for M365 apps from Desktop client machine only. NOTE: this signout is only machine not for Mobile M365 applications. I want to suppress the prompts when script execution is started. Please find the below:

function signoutofc {

function Send-Keys {

    param(

        [string]$keys,

        [int]$sleepSeconds = 1

    )

    Add-Type -AssemblyName System.Windows.Forms

    [System.Windows.Forms.SendKeys]::SendWait($keys)

    Start-Sleep -Seconds $sleepSeconds

}

$office365Path = "C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE"

$office2019Path = "C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE"

$office2016Path = "C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE"

$office365Path64 = "C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE"

$office2019Path64 = "C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE"

$office2016Path64 = "C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE"

$officeVersion = ""

$displayName = ""

try {

    $office365 = Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\O365ProPlusRetail*' -ErrorAction SilentlyContinue

    $office2019 = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ProPlus2019Retail*' -ErrorAction SilentlyContinue

    $office2016 = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PROPLUSRetail*' -ErrorAction SilentlyContinue

    $officeWord = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{90160000-008C-0000-0000-0000000FF1CE}*' -ErrorAction SilentlyContinue

    if ($office365) {

        $officeVersion = "Office365"

        $displayName = $office365.DisplayName

    } elseif ($office2019) {

        $officeVersion = "Office2019"

        $displayName = $office2019.DisplayName

    } elseif ($office2016) {

        $officeVersion = "Office2016"

        $displayName = $office2016.DisplayName

    } elseif ($officeWord) {

        $officeVersion = "WordStandalone"

        $displayName = $officeWord.DisplayName

    } else {

        $officeVersion = "Unknown"

        $displayName = "Not Detected"

    }

} catch {

    

}

if ($officeVersion -ne "") {

    $officePath = ""

    switch ($officeVersion) {

        "Office365" {

            if (Test-Path $office365Path) {

                $officePath = $office365Path

            } elseif (Test-Path $office365Path64) {

                $officePath = $office365Path64

            }

        }

        "Office2019" {

            if (Test-Path $office2019Path) {

                $officePath = $office2019Path

            } elseif (Test-Path $office2019Path64) {

                $officePath = $office2019Path64

            }

        }

        "Office2016" {

            if (Test-Path $office2016Path) {

                $officePath = $office2016Path

            } elseif (Test-Path $office2016Path64) {

                $officePath = $office2016Path64

            }

        }

    }

    if ($officePath -ne "" -and (Test-Path $officePath)) {

        Start-Process -FilePath $officePath -WindowStyle Hidden -ErrorAction SilentlyContinue

        

        Start-Sleep -Seconds 1 -ErrorAction SilentlyContinue

        Send-Keys '%L' -sleepSeconds 1 Hide

        Send-Keys '%f' -sleepSeconds 1 Hide

        Send-Keys 'D' -sleepSeconds 1 Hide

        Send-Keys 'E' -sleepSeconds 1 Hide

        if ($officeVersion -eq 'Office365') {

            Send-Keys '{ENTER}' -sleepSeconds 1 Hide

        } else {

            Send-Keys '{Left}' -sleepSeconds 1 Hide 

            Send-Keys '{ENTER}' -sleepSeconds 0 Hide

        }

        Start-Sleep -Seconds 1 -ErrorAction SilentlyContinue

        Stop-Process -Name WINWORD -Force -ErrorAction SilentlyContinue

    }

}

}

signoutofc

If there is any other alternative, please suggest that as well also only for Desktop M365 solution.

Also, we tried removing the account from this registry- Computer\HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Common\Identity\Identities

did not helped. PLease help me to resolve this.

Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Client for IT Pros | User experience | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 36,401 Reputation points
    2024-08-13T02:40:04.99+00:00

    I want to suppress the prompts when script execution is started

    What prompts?

    I don't think that you can use "-WindowStyle Hidden" and send keys to it. I have always used appactivate to bring the app window to the foreground. And I don't see how that "Hide" option is going to do anything.

    Here is an example that launches a command prompt, does a directory list and then exits.

    # https://stackoverflow.com/questions/62011653/powershell-send-keys-to-3rd-party-application-in-the-background
    function Send-Keys {
        param(
            [string]$keys,
            [int]$sleepSeconds = 1
        )
        [System.Windows.Forms.SendKeys]::SendWait($keys)
        Start-Sleep -Seconds $sleepSeconds
    }
    Add-Type -AssemblyName System.Windows.Forms
    add-type -AssemblyName microsoft.VisualBasic
    Start-Process -FilePath "c:\windows\system32\cmd.exe" 
    Start-Sleep -Seconds 3
    [Microsoft.VisualBasic.Interaction]::AppActivate("cmd.exe") 
    Send-Keys "dir{ENTER}" -sleepSeconds 5
    Send-Keys "exit" -sleepSeconds 3
    Send-Keys "{ENTER}" 
    

    I do not have Office installed so I am unable to test that.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.