SpyglassMTG Blog

  • Blog
  • Using the Microsoft Graph PowerShell SDK: How to Pass Optional Parameters

Using the Microsoft Graph PowerShell SDK: How to Pass Optional Parameters

Using the Microsoft Graph PowerShell SDK: How to Pass Optional Parameters

I was working on a script for a client to solve a typical problem – bulk create of users in Azure AD. I decided to adopt the new Graph PowerShell SDK (see Microsoft Graph PowerShell SDK overview | Microsoft Learn).

The user information was being driven from a CSV file, which was sparsely populated – meaning that some fields such as Department, Address, or Mobile Phone fields were missing.

The New-MgUser command supports all the properties I wanted to set, such as First, Last, Department, Address etc.

However, there is a major issue with using a parameter such as -Department and specifying an empty string as the value – the request fails with an error message stating that the field value is not valid. So, optional values to the New-MgUser command was just not working for me.

Frustrated, with this lack of functionality, I had to adopt a different approach – Splatting (see about Splatting - PowerShell | Microsoft Learn). Basically, build a Hash table of the parameters you want to pass and then call the Command. For example:

ian1-1

Once you have built the parameter hash table you can then call the command:

ian 2

The result of the call is an object that describes the user you just created – the most importing thing being the ID (GUID) for the user object in Azure AD.

Hopefully this technique for passing optional parameters to the New-MgUser command will help someone else. If you have any further questions, please contact us today!