Hi Everyone!
I recently had the opportunity to use PowerShell to update AzureAD user attributes. This is different from what I normally do as we still leverage an on-prem AD setup.
I’d never used the command before but I know PowerShell and I’m fairly confident with the AD PowerShell commands.
The mission at hand was this: Update AzureAD user attributes so that the Marketing department had new address information
The first and rather dirty method I put together as a proof-of-concept is below:
I know right, it’s ugly. It’s lacking any form of error checking, there’s no host output and it’s hard to read.
What I did next was put my code behind a few checks. You can see the improved code below:
This was looking much better, it handles error nicely but there is still room for improvement…
I want to implement splatting and also look into ways to speed the script up!
I wanted to take a look into speed first. I know there are subtle different between using the -filter parameter and piping the results into a Where-Object commandlet. Lets run some tests!
I ran the below commands 5 times to get an average using Measure-Command and outputted in total miliseconds:
Command | Get-AzureADUser -ErrorAction Stop | Where-Object {$_.Department -eq ‘Development’} | Get-AzureADUser -Filter “Department eq ‘Development'” -ErrorAction Stop |
#1 | 1311.6669 | 6630.8861 |
#2 | 1769.6253 | 7973.5126 |
#3 | 2122.8749 | 6060.9699 |
#4 | 1963.6512 | 5315.6691 |
#5 | 3437.268 | 5783.7616 |
Crazy! Switching from the -filter parameter to using the pipeline more than halved the time it took for the command to run!
Next was to build the hashtable for splatting in the Set-AzureADUser parameters before building the final version. This was simple done by using the below code:
This now means I can simplify the Set-AzureADUser command.
You can find the full and finished script below:
Enjoy!