Microsoft Graph PowerShell - Backticks vs. Splatting vs. Class Objects

October 22, 2021
3 min read
Syndicated

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!!

Liam Cleary

Liam Cleary

Liam began his career as a computer trainer. He quickly realized that programming, breaking and hacking were much more fun. Liam spent the next few years working within core infrastructure and security services. He is now the founder and owner of SharePlicity, a consulting company focusing on Microsoft 365 and Azure technology. His role within SharePlicity is to help organizations implement Microsoft 365 and Azure technology to enhance internal and external collaboration, document, and records management, automate business processes, and implement security controls and protection. He is a long-time Microsoft MVP and Microsoft Certified Trainer, focusing on architecture, security and crossing the boundary into software development. Over the past few years, his specialty has been security in Microsoft 365, Azure and its surrounding platforms. Liam also creates online training courses for Pluralsight, LinkedIn Learning and Cloud Academy, and he teaches multiple Microsoft certification courses for Opsgility and Microsoft. You can find him at user groups and conferences, teaching classes, offering advice, spending time in the community, teaching his kids how to code, raspberry PI programming, hacking the planet, building Lego robots, or coaching soccer. You may also find him running races in the dark, hiking, or mountain biking at breakneck speeds.