It may not make sense for a script to change that setting, unless the aim is just to change the global Preferences setting.
If the aim is to temporarily disable it while changing folder, or similar, then it probably wouldn't be a good idea to change the global setting as it could interfere with other folder changes that are happening in parallel.
If you're making a script that needs to change folder while keeping the filter, a better way to do the same thing would be to record the filter being changing folder, then change folder and re-apply the filter (or clear the filter, if you want the opposite).