You can use the ArcGIS API for Python to script the administration of your portal. The ArcGIS API for Python Sample Notebooks site provides samples in the form of a Jupyter Notebook. Some samples are also available as stand-alone Python scripts in the accompanying GitHub SDK repository.
The following samples are available: