The below list a continued collection of random PowerShell Code Snippets & Functions that I use for various things. If there is anything that you wish to have added, let me know in the comments below!


Get-CsHostedMigrationURL – PowerShell Function

If you are wanting to move users to Microsoft Teams from Skype for Business and are not using a .onmicrosoft.com admin account, you need to manually specify the hosted migration URL. Unfortunately, the newest Teams PowerShell (2.4.x) breaks Get-CsTenant and is no longer able to provide this. The below method is by far one of the best I’ve been able to come up with as it doesn’t even need the Teams PowerShell Module! This was updated on 3/23/2023 to add support for GCC, GCC High, DOD, & DOD High customers.

How to Run:

Get-CsHostedMigrationURL -Domain microsoft.com -Environment CC
Options are CC, GCC, GCCHigh, DOD, DODHigh, or GOV
Alternatively running the command as the following will default to commercial cloud
Get-CsHostedMigrationURL microsoft.com 

Function Code:

function Get-CsHostedMigrationURL
{
    param(
        [Parameter(Mandatory=$true)][string]$Domain,
        [Parameter(Mandatory=$false)][string]$Environment
    )

    if (($Environment -eq "GCC") -or ($Environment -eq "GCCHigh") -or ($Environment -eq "GOV"))
        {
            $HMSID = try {
            (((Invoke-WebRequest -Uri "https://webdir.online.gov.skypeforbusiness.us/Autodiscover/AutodiscoverService.svc/root?originalDomain=$($Domain)").content -split "webdir")[3] -split ".online")[0]
                    }
                catch
                    {
                        Write-Error "Failed to get the Hosted Migration URL. The exception caught was $_" -ErrorAction Stop
                    }
 
            $MigrationURL = "https://admin" + "$($HMSID)" + ".gov.skypeforbusiness.us/HostedMigration/hostedmigrationService.svc"
            $MigrationURL
        }
    elseif (($Environment -eq "DOD") -or ($Environment -eq "DODHigh"))
        {
            $HMSID = try {
            (((Invoke-WebRequest -Uri "https://webdir.online.dod.skypeforbusiness.us/Autodiscover/AutodiscoverService.svc/root?originalDomain=$($Domain)").content -split "webdir")[3] -split ".online")[0]
                    }
                catch
                    {
                        Write-Error "Failed to get the Hosted Migration URL. The exception caught was $_" -ErrorAction Stop
                    }
 
            $MigrationURL = "https://admin" + "$($HMSID)" + ".dod.skypeforbusiness.us/HostedMigration/hostedmigrationService.svc"
            $MigrationURL
        }
    else
        {
            $HMSID = try {
            (((Invoke-WebRequest -Uri "https://webdir.online.lync.com/Autodiscover/AutodiscoverService.svc/root?originalDomain=$($Domain)").content -split "webdir")[3] -split ".online")[0]
                    }
                catch
                    {
                        Write-Error "Failed to get the Hosted Migration URL. The exception caught was $_" -ErrorAction Stop
                    }
 
            $MigrationURL = "https://admin" + "$($HMSID)" + ".online.lync.com/HostedMigration/hostedmigrationService.svc"
            $MigrationURL
        }
}


Get-TenantID – PowerShell Function

This provides the GUID of a Microsoft 365 Tenant ID. Simply input any tenant domain name. Thanks @Richard Brynteson

How to Run:

Get-TenantID -Domain Microsoft.com

Function Code:

function Get-TenantID
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory)]
        [String]$Domain
    )

    try {
            (Invoke-WebRequest https://login.windows.net/$($Domain)/.well-known/openid-configuration|ConvertFrom-Json).token_endpoint.Split('/')[3]
        }
    catch
        {
            Write-Error "Failed to get the Tenant ID. The exception caught was $_" -ErrorAction Stop
        }
}


Generate-NVKey – PowerShell Function

This PowerShell Function generates a random 32-bit Hex string that can be used for various tasks such as security. This is based on the output of New-Guid however since the 13th character is always a 4, we have to modify this to give us a completely random yet unique response.

function Generate-NVKey
	{
		function New-12Bit
			{
				((new-guid).guid).replace('-','').SubString(0,12)
			}
		(((New-12Bit) + (New-12Bit) + (New-12Bit)).ToUpper()).SubString(0,32)
	}
Generate-NVKey

If you would like an output that is of any value 0-36 characters in length, change the 32 in the above function to the desired number.


Example Output:


Redeem Microsoft 365 Business Subscription Link

https://portal.office.com/Commerce/ProductKeyRedeem.aspx