While writing a PowerShell script that uses items in System.DirectoryServices.AccountManagement, I noticed a discrepancy between when my script runs on PowerShell Desktop vs PowerShell Core.
This command works on PowerShell Core without explicit assembly loads:
$context = [System.DirectoryServices.AccountManagement.PrincipalContext]::new(
[System.DirectoryServices.AccountManagement.ContextType]::Domain,
$Domain)
However, the exact same command fails on PowerShell Classic with:
Unable to find type [System.DirectoryServices.AccountManagement.PrincipalContext].
However, if I manually load the type (via Add-Type -AssemblyName System.DirectoryServices.AccountManagement), it works fine on PowerShell Classic.
What's striking me as odd is my script uses plenty of other .NET Assemblies, all of which load fine without an explicit Add-Type call, in both Classic and Core.
Is there a clear set of rules for which assemblies must be imported via Add-Type vs which can be implicitly loaded?