Try running below PowerShell script:
$userName = "user@tenant.onmicrosoft.com"
$passWord = "YourSPOPassword"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com/" -Credentials $cred
#Example as below:
Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com/" -Credentials $cred
try
{
$web = Get-PnPWeb
#0 - Inherit, #1 - AllPages, #2 - ModernOnly, #3 - Hidden enums for Global (Modern Search)
$web.SearchBoxInNavBar=2
$web.Update()
Invoke-PnPQuery
Write-Host "The changes have been updated successfully." -BackgroundColor Green
}
catch
{
$ErrorMessage = $_.Exception.Message +"in applying search setting changes!: "
Write-Host $ErrorMessage -BackgroundColor Red
Write-Log $ErrorMessage
}
Write-Host "The script execution has been completed successfully."
Source: Configure SharePoint search box in office 365 suite bar step by step