Adding and removing individual login items from Terminal

There is a way to use the defaults command-line tool to add items to the Login Items list of the Accounts or Users & Groups System Preference panel:
defaults write loginwindow AutoLaunchedApplicationDictionary -array-add '{ "Path" = "/path/to/itemname"; "Hide" = 0; }'

Unfortunately, Apple provides no easy way to use defaults to remove a given login item from the list. While you can remove all login items by deletingAutoLaunchedApplicationDictionary, that’s usually not what you want to do.

Luckily, there’s another way to do it that does allow removal of individual items as well as other things.

A nice work-around solution is to instead use the osascript command-line tool to do it with simple AppleScript commands:

Add an item:

osascript -e 'tell application "System Events" to make login item at end with properties {path:"/path/to/itemname", hidden:false}'

Remove an item:

osascript -e 'tell application "System Events" to delete login item "itemname"'

List all items:

osascript -e 'tell application "System Events" to get the name of every login item'