Here is some code I used for a migration a while back in order to create portal users. You should be able to modify the license type but I'm not aware that you can specify the password. I marked the users inactive by default to avoid license overages. This code was called from the onAfterInsert event in the contact trigger. If using a batch job, you may have to limit the scope to avoid hitting limits. Remember that once a user license is created, that record will be there forever but will be ignored by licensing if inactive.
This link provides additional user properties: API User object
/**
* Create new partner user in inactive state
*/
@future static void createUser(String contactId, String email, String firstName, String lastName, String userName, String phone, String mobilephone, String profileId,String roleId,String externalId) {
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.EmailHeader.triggerUserEmail = false;
User u = new User(alias = (lastname.length()>8?lastname.substring(0,8):lastname), email=email,
lastname=lastname, firstname=firstname, Phone=phone, MobilePhone=mobilephone,
emailencodingkey='UTF-8', languagelocalekey='en_US',
localesidkey='en_US', profileid = profileId, contactId=contactId,
timezonesidkey='America/Los_Angeles', username=username,ExternalId__c=externalId);
u.setOptions(dmo);
//
// Mark user inactive by default, manually activate current designers in UI
// This will avoid license overages.
//
u.IsActive = false;
try {
insert u;
}
catch(Exception ex) {
//write to log file here
}
}