Editing System Preferences via Terminal

Am looking to edit system preferences settings via the command line but cannot for the life of me find the correct names of variables.

In Login Options under Users & Groups, I would like to be able to change the Display login window from list of users to 'name and password'.
In Date & Time, I would like to know how I change the time to a server we use. Enable fields in Sharing Setup Energy Saver settings

It would be great if someone can help me out or point me in the right direction as I know you edit the preferences .plists but its knowing what to add or finding the preferred naming conventions.

Thanks.


First off, you can check out a website that lists a lot of these things: http://secrets.blacktree.com/

I, however, just took a brute-force solution:

Copy the Preferences folder

$ cp -r /Library/Preferences before

Launch System Preferences. Make a change via the GUI. Probably best to do one change at a time, e.g. I changed "Display Login Window as:" from "List of users" to "Name and password". Quit System Preferences.

Copy the Preferences folder again:

$ cp -r /Library/Preferences after

See which files changed:

$ diff -ur before after
Binary files before/Preferences/com.apple.loginwindow.plist and after/Preferences/com.apple.loginwindow.plist differ

Compare the two versions. Since they are binary files, you'll need to convert them to XML for comparison. I use an alias for this:

$ alias plist='plutil -convert xml1 -o /dev/stdout'
$ diff -u <(plist before/Preferences/com.apple.loginwindow.plist) <(plist after/Preferences/com.apple.loginwindow.plist)
--- /dev/fd/63  2013-01-23 18:20:29.000000000 +0200
+++ /dev/fd/62  2013-01-23 18:20:29.000000000 +0200
@@ -9,7 +9,7 @@
    <key>RetriesUntilHint</key>
    <integer>3</integer>
    <key>SHOWFULLNAME</key>
-   <false/>
+   <true/>
    <key>lastUser</key>
    <string>loggedIn</string>
    <key>lastUserName</key>

At this point we have located the setting. Confirm we have it with defaults:

$ defaults read /Library/Preferences/com.apple.loginwindow SHOWFULLNAME
1
$ sudo defaults write /Library/Preferences/com.apple.loginwindow SHOWFULLNAME -bool false
$ defaults read /Library/Preferences/com.apple.loginwindow SHOWFULLNAME
0

Launch System Preferences and confirm it changed.


sudo opensnoop -n cfprefsd shows what property lists are being accessed. You can also just sort the preference files by modification date: ls -t {~,}/Library/Preferences/{ByHost,}.

For preferences in the user domain, you can also run defaults read > temp, change some preference, and run diff temp <(defaults read).


You can use AppleScript to control many of the System Preferences properties. See https://www.macosxautomation.com/applescript/features/system-prefs.html for examples.

Depending on what you want to do, you can create a script to do things like

$ set-setting "dock preferences" "autohide:true, magnification:false"

Use the Script Editor.app to run and debug code in case the property labels have changed in newer macOS versions.