To add consent programmatically, you can use the appRoleAssignment endpoint (for application permissions): https://learn.microsoft.com/en-us/graph/api/serviceprincipal-post-approleassignments?view=graph-rest-1.0&tabs=http
Or the oAuth2PermissionGrant endpoint for delegate permissions: https://learn.microsoft.com/en-us/graph/api/oauth2permissiongrant-post?view=graph-rest-beta&tabs=http
If you prefer to use the Graph SDK for PowerShell, I have a more detailed writeup here: https://www.michev.info/blog/post/6080/how-to-manage-entra-id-delegate-permissions-for-specific-users