Enable And Remove Microsoft 365 Search Bar In SharePoint Online Site Using Powershell

Introduction 

 
This blog will help to remove the Microsoft365 search bar or enable the Microsoft365 search bar in SharePoint online.
 
To connect the SharePoint Online site in Powershell, run the below command:
  1. Connect-PnPOnline -Url "Enter Site URL" -UseWebLogin  

Remove Microsoft365 Search bar in SharePoint Online site

 
Run the below command to remove the Microsoft365 search bar in the current site, which can only affect the changes in the current SharePoint Online site.
  1. Set-PnPSearchSettings -SearchBoxInNavBar Hidden -Scope Web  
Run the below command to remove the Microsoft365 search bar in the Site Collection and its respected subsites.
  1. Set-PnPSearchSettings -SearchBoxInNavBar Hidden -Scope Site  
Note: The above command must run under the Site Collection, which helps to affect the Site Collection and its respected subsites.
 
After executing the command in the Powershell, it will display a confirmation dialog box.
 
Once we have chosen “Yes” in the dialog box, the remove command will be executed.
 
Check the below screenshot. Microsoft365 search is removed from the top bar on the site.
 

Enable Microsoft365 Search Bar in SharePoint Online site

 
Run the below command to enable the Microsoft365 search bar in the current site, which can only enable the changes in the current site.
  1. Set-PnPSearchSettings -SearchBoxInNavBar ModernOnly -Scope Web  
Run the below command to enable Microsoft365 search bar in the Site Collection and its respected subsites.
  1. Set-PnPSearchSettings -SearchBoxInNavBar ModernOnly -Scope Site  
Note
The above command must run under the Site Collection, which helps to enable the Site Collection and its respected subsites.
 
Check the above screenshot, Microsoft365 search is enabled in the top bar in the SharePoint Online site.
 
Please refer to this page to learn more about "Set-PnpSearchSetttings".