I had no programming experience three months ago, so PowerShell initially looked like complete gibberish. After practicing consistently, concepts such as conditionals, filtering, Where-Object, Select-Object, loops, splatting, CmdletBinding, and try/catch are finally beginning to click. I built this function to create a user in Microsoft Entra ID and would appreciate constructive feedback. What should I improve in the code, and what topics would be best to learn next?
The function accepts a display name, user principal name, mail nickname, and account-enabled setting. It generates a temporary password, builds a parameter hashtable, calls New-MgUser, and returns a custom object containing the new user's details. I also included verbose messages and error handling.
3 Answers
Nice progress, especially for only three months of practice. A few things are worth tightening up: use a [switch] parameter when you want a presence-based option rather than a [bool], and avoid unnecessary string concatenation when building the temporary password. The format operator or an interpolated string is clearer. Also, capture the object returned by New-MgUser and use its verified properties when creating your output object instead of repeating the input values. Add comment-based help so users can run Get-Help on the function. Finally, the call should pass your hashtable with the splatting operator, for example New-MgUser @UserConfig.
Functions are a good habit because they make scripts more structured and readable. Keep practicing parameter validation, comment-based help, pipeline support, and consistent error handling. Those features will become increasingly useful as your automation grows.
The only downside is that callers sometimes have to inspect the function body to understand what it actually does, so clear help and descriptive parameter names are especially important.
The password-generation approach is not cryptographically secure, so it should not be used for sensitive production accounts. Use a secure random mechanism or, preferably, let the identity platform generate or manage the initial credential when possible. It is also worth learning about temporary access passes and other identity-platform-native onboarding methods.
Good point, thank you. I'll look into secure password generation and temporary access passes next.

Thanks! I'm trying to build these habits early so I do not have to fix a mess when my automation scripts become much larger.