Windowsでスタートアップ アプリをコマンドで無効化する方法

Windowsでスタートアップ アプリをコマンドで無効化する方法。

Windowsには、OSを起動したときやユーザーがサインインしたときに、特定のアプリやプログラムを自動的に実行させる「スタートアップ」という機能があり、スタートアップに登録しているアプリは、Windowsの「設定」>「アプリ」>「スタートアップ」や、タスクマネージャーから有効/無効を切り替えることができますが、コマンドで切り替えたいケースもあるのではないでしょうか。

そこでここでは、スタートアップに登録されているアプリをコマンド操作(Powershell)で無効化する方法を解説します。

スタートアップアプリを無効化する

スタートアップに登録されている指定のアプリを無効化するためのコマンドは提供されていないので、ここではPowerShellからUI Automationを使ってWindowsの設定画面を操作して、指定のアプリのスタートアップを無効化する方法を紹介します。

UIオートメーション(UI Automation)とは、Windows上で実行されているアプリケーション画面にあるボタンやテキストボックスといった要素を、外部から識別・操作するための仕組みです。

function Disable-StartupApp {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [array]$apps
    )

    Add-Type -AssemblyName UIAutomationClient
    Add-Type -AssemblyName UIAutomationTypes

    $UIAElement = [System.Windows.Automation.AutomationElement]
    $UIATreeScope = [System.Windows.Automation.TreeScope]
    $UIACondition = [System.Windows.Automation.PropertyCondition]
    $UIAAndCond = [System.Windows.Automation.AndCondition]

    Start-Process "ms-settings:startupapps"

    $root = $UIAElement::RootElement
    $winCond = New-Object $UIACondition($UIAElement::ClassNameProperty, "ApplicationFrameWindow")
    $nameCond = New-Object $UIACondition($UIAElement::NameProperty, "設定")
    $andCond = New-Object $UIAAndCond($winCond, $nameCond)

    $count = 0
    while ($count -lt 15) {
        $settingsWin = $root.FindFirst($UIATreeScope::Children, $andCond)
        if ($settingsWin) {
            break
        }
        Start-Sleep -Seconds 1
        $count++
    }

    foreach ($app in $apps){
        $targetCond1 = New-Object $UIACondition($UIAElement::NameProperty, $app)
        $targetCond2 = New-Object $UIACondition($UIAElement::LocalizedControlTypeProperty, "toggle switch")
        $andCond = New-Object $UIAAndCond($targetCond1, $targetCond2)

        $count = 0
        while ($count -lt 15) {
            $toggle = $settingsWin.FindFirst($UIATreeScope::Descendants, $andCond)
            if ($toggle) {
                break
            }
            Start-Sleep -Seconds 1
            $count++
        }

        if ($toggle) {
            $togglePattern = $toggle.GetCurrentPattern([System.Windows.Automation.TogglePattern]::Pattern)
            $currentState = $togglePattern.Current.ToggleState
            if ($currentState -eq 'on'){
                $togglePattern.Toggle()
            }
        } else {
            Write-Warning "$($app)は、存在しません。"
        }
    }

    Start-Sleep 3
    Stop-Process -Name 'SystemSettings'
}

Disable-StartupApp -apps 'Microsoft Edge','Microsoft OneDrive'

上のPowerShellスクリプトでは、Windowsの「設定」>「アプリ」>「スタートアップ」画面を開いて指定アプリのスタートアップを無効化する関数を作成・実行しており、例として「Microsoft Edge」と「Microsoft OneDrive」のスタートアップを無効化しています。

また、51行目の「if ($currentState -eq 'on')」を「if ($currentState -eq 'off')」と書き換えれば、無効化されているアプリを有効化する関数にすることもできます。

あとがき

Windowsマシンの設定で、一部のスタートアップアプリを無効化したい場面は意外と多いですが、コマンド操作で対応しようとすると、これまでだとスタートアップの登録を削除するしかありませんでしたが、PowerShellからUI Automationを使ってWindowsの設定画面を操作する方法であれば、登録自体を削除することなく指定のアプリのスタートアップを無効化できます。