Change global people-picker settings programatically

Supposed that you want to change the peoplepicker-searchadcustomquery property via STSADM and, for one reason or the other, you get an error message like the following:

 & was unexpected at this time.

Still, you are 100% sure that the LDAP query you have supplied is correct. What to do next?

Well, if you're planning to make the change for a single web application (or just a couple of web applications), you could use the SPWebApplication.PeoplePickerSettings.ActiveDirectoryCustomQuery property. But what if you want to make the change globally (such as using the STSADM command without mentioning the -url parameter)? Well, the following code may be of help:

 static readonly string propName = "peoplepicker-searchadcustomquery";
static readonly string propValue = ""; // valid LDAP query goes here
static void Main(string[] args) {
  SPGlobalAdmin glAdm = new SPGlobalAdmin();
  Console.WriteLine(glAdm.Config.Properties[propName]);
  try {
    glAdm.Config.Properties[propName] = propValue;
    glAdm.Config.Properties.Update();
  }
  catch (Exception ex) {
    Console.WriteLine(ex.Message);
  }
  Console.WriteLine(glAdm.Config.Properties[propName]);
}