The script below can be used in order to enable versioning in all SharePoint document libraries and also to set how many major versions to retain and how many minor versions to maintain.
it’s important to keep in mind when choosing how many minor versions to retain that the setting actually specifies how many ‘Major Versions’ drafts’ should be retained and not a hard limit on how many ‘draft versions’ will be kept.