To allow the user to undo actions that are performed by a custom editor script you can use Undo.RegisterUndo.
foreach (var item in Selection.transforms)
{
Undo.RegisterUndo(item, "Move " + item.name);
var pos = item.position;
pos = new Vector3(activeObject.transform.position.x, pos.y, pos.z);
item.transform.position = pos;
}