Good evening,
just for my curiousity: Are you creating a fresh sonarqube instance per scan? If so, why? :)
If not, just use an authentication token? https://docs.sonarsource.com/
If yes, sounds like you want to do an oidc login with username/password, change the password and login again? Can't understand your flow fully right now.
To change your password by commandline / script, use the api:
curl -u admin:admin -X POST "http://localhost:9000/api/users/change_password?login=admin&previousPassword=admin&password=XXXXXXXXX"
Source, Documentation and Api-Documentation
Best regards