Skip to content

Set-SearchConfig

Windows: Configures Windows Search and Cortana settings

#Requires -Version 5.1

[CmdletBinding()]
Param (
    [bool]$AllowCortana,

    [bool]$AllowLocation,

    [bool]$DisableWebSearch,

    [string]$ComputerName = $env:COMPUTERNAME,

    [pscredential]$Credential
)

Process {
    try {
        $scriptBlock = {
            Param($Cortana, $Location, $Web, $BoundParams)
            $regPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\Windows Search"
            if (-not (Test-Path $regPath)) {
                New-Item -Path $regPath -Force | Out-Null
            }
            
            if ($BoundParams.ContainsKey('AllowCortana')) {
                $val = if ($Cortana) { 1 } else { 0 }
                Set-ItemProperty -Path $regPath -Name "AllowCortana" -Value $val -Force -ErrorAction Stop
            }
            if ($BoundParams.ContainsKey('AllowLocation')) {
                $val = if ($Location) { 1 } else { 0 }
                Set-ItemProperty -Path $regPath -Name "AllowSearchToUseLocation" -Value $val -Force -ErrorAction Stop
            }
            if ($BoundParams.ContainsKey('DisableWebSearch')) {
                $val = if ($Web) { 1 } else { 0 }
                Set-ItemProperty -Path $regPath -Name "DisableWebSearch" -Value $val -Force -ErrorAction Stop
            }
        }

        if ($ComputerName -ne $env:COMPUTERNAME) {
            $invokeParams = @{
                'ComputerName' = $ComputerName
                'ScriptBlock'  = $scriptBlock
                'ArgumentList' = @($AllowCortana, $AllowLocation, $DisableWebSearch, $PSBoundParameters)
                'ErrorAction'  = 'Stop'
            }
            if ($null -ne $Credential) {
                $invokeParams.Add('Credential', $Credential)
            }

            Invoke-Command @invokeParams
        }
        else {
            &$scriptBlock -Cortana $AllowCortana -Location $AllowLocation -Web $DisableWebSearch -BoundParams $PSBoundParameters
        }

        $result = [PSCustomObject]@{
            ComputerName = $ComputerName
            Action       = "SearchConfigured"
            Status       = "Success"
            Timestamp    = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $result
    }
    catch {
        throw
    }
}

If set, enables Cortana search.

If set, allows search and Cortana to use location data.

If set, disables web results in Windows Search.

Specifies the name of the target computer. Defaults to the local computer.

Specifies a PSCredential object for remote connection.

An interactive directory of PowerShell scripts.