I am sure by now you know what I am talking about here—the three approaches to creating PowerShell scripts to reuse as needed.

I have often had this conversation about what is best, and it reminds me very much of the “spaces” versus “tabs” for code like C#. Some like backticks, some prefer splatting, and some enjoy class objects.

To help you decide what to use, let us look at each one.

Backticks

The Backtick (‘) operator is also called the word-wrap operator. It allows writing commands on multiple lines. You can also use it for new line (n) or tab (t) in typical sentences.

Create Azure Active Directory User using Backticks

$password = New-Object `
	-TypeName Microsoft.Graph.PowerShell.Models.MicrosoftGraphPasswordProfile
$password.Password = "Pass@word1"
$user = "apun@m365x969864.onmicrosoft.com"
$displayname = "Apu Nahasapeemapetilon"

New-MgUser `
	-DisplayName $displayname `
	-PasswordProfile $password `
	-UserPrincipalName $user `
	-AccountEnabled `
	-MailNickName $displayname.Replace(' ','')

Splatting

The Microsoft documentation states that “Splatting is a method of passing a collection of parameter values to a command as a unit. Windows PowerShell associates each value in the collection with a command parameter.”

In reality, it means that you create a single variable that contains all of the property key-value pairs you need and pass that to the command instead.

Create Azure Active Directory User using Splatting

$password = New-Object `
	-TypeName Microsoft.Graph.PowerShell.Models.MicrosoftGraphPasswordProfile
$password.Password = "Pass@word1"
$user = "barts@m365x969864.onmicrosoft.com"
$displayname = "Bart Simpson"

$Params = @{
	DisplayName  = $displayname;
	PasswordProfile = $password;
	UserPrincipalName = $user;
	AccountEnabled = $true;
	MailNickName = $displayname.Replace(' ','');
}

New-MgUser @Params

Instantiate Class Objects

Instantiating class objects are more complicated as they require you to know what the implementing class is for a specific object. This approach aims to create an object containing all the required properties and simplify it.

To identify what “TypeName” to use, you can execute the following command for the cmdlet you wish to utilize.

Get-MgUser | Get-Member

This will return the “TypeName” for the object you are trying to create.

Create Azure Active Directory User using a Class Object

$password = New-Object `
	-TypeName Microsoft.Graph.PowerShell.Models.MicrosoftGraphPasswordProfile
$password.Password = "Pass@word1"
$userprincipalname = "lisas@m365x969864.onmicrosoft.com"
$displayname = "Lisa Simpson"

$user = New-Object `
	-TypeName Microsoft.Graph.PowerShell.Models.MicrosoftGraphUser1
$user.DisplayName  = $displayname;
$user.PasswordProfile = $password;
$user.UserPrincipalName = $userprincipalname;
$user.AccountEnabled = $true;
$user.MailNickName = $displayname.Replace(' ','');

New-MgUser -BodyParameter $user

Additional Approach Provided by @JustinWGrote

Original example: https://gist.github.com/JustinGrote/02ac004ace5884cc97d8094ab0a92ff4

Modified example below:

Using Namespace Microsoft.Graph.PowerShell.Models
[MicrosoftGraphUser1]@{
	DisplayName = 'Marge Simpson'
	UserPrincipalName = 'marges@m365x969864.onmicrosoft.com'
	MailNickname = 'marges'
	AccountEnabled = $true
	PasswordProfile = [MicrosoftGraphPasswordProfile]@{
		Password = 'Pass@word1'
	}
} | New-MgUser

There you have it, three different ways of executing PowerShell to perform the same action. Choosing between each approach is up to you as the creator of the PowerShell scripts. I like using splatting and class objects where I can. However, the most common and easiest way of structuring PowerShell code is still backticks. The good news is, you get to choose what works for you. Now go forth and write some great PowerShell!!