Skip to content

Instantly share code, notes, and snippets.

@aflyen
Created December 6, 2022 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aflyen/f6d5b3900afeeb8d795b099e6b68ab51 to your computer and use it in GitHub Desktop.
Save aflyen/f6d5b3900afeeb8d795b099e6b68ab51 to your computer and use it in GitHub Desktop.
Get a unique Microsoft 365 Group alias for use when creating new groups in automated solutions
function Get-CorpMicrosoft365GroupAlias
{
Param(
[Parameter(Mandatory=$true)]
[string]$Alias
)
$AliasFound = $false
$Retries = 0
$UniqueAlias = $Alias
while ($AliasFound -eq $false)
{
$Group = Get-PnPMicrosoft365Group -Identity $UniqueAlias
# No group found so the alias is available
if ($Group -eq $null)
{
$AliasFound = $true
return $UniqueAlias
}
# If no unique alias has been found in 20 retries cancel
if ($Retries -eq 20)
{
throw "Unable to find unique alias"
}
# Create attempt of unique alias by adding random 4 numbers at the end
$UniqueAlias = "$($Alias)$(Get-Random -Minimum 1000 -Maximum 9999)"
$Retries++
}
return $UniqueAlias
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment