How to add a component to a prefab and then save the prefab

Have a gameobject that has many levels of children and I need to add specific components to the children only, not the parents. I know how to do this by hand, but it takes a long time on larger gameobjects. I know how to add components to a gameobject in code, but the problem is, I don’t know how to save that gameobject as a new, updated prefab.

Any suggestions on how to save an updated prefab? (possibly with keeping the original prefab untouched)

Any and all help is greatly appreciated. Thank you.

Inside the editor, you can use Unity - Scripting API: PrefabUtility.CreatePrefab . Note the replacement options available to this method.

Thank you so much Cherno. Could have sworn I went down that path before with no luck, but this time it worked perfectly.