How do I conditionally add a class with Add-Type -TypeDefinition if it isn’t added already?

This technique works well for me: if (-not ([System.Management.Automation.PSTypeName]’MyClass’).Type) { Add-Type -TypeDefinition ‘public class MyClass { }’ } The type name can be enclosed in quotes ‘MyClass’, square brackets [MyClass], or both ‘[MyClass]’ (v3+ only). The type name lookup is not case-sensitive. You must use the full name of the type, unless it is part … Read more

Can you remove an Add-ed Type in PowerShell again?

Like the others say, this is a .NET behavior. Assemblies loaded into an AppDomain cannot be unloaded. Only the AppDomain can be unloaded, and powershell uses a single appdomain. I blogged a bit about this some years ago: https://web.archive.org/web/20170707034334/http://www.nivot.org/blog/post/2007/12/07/WhyAppDomainsAreNotAMagicBullet When I test like this, I usually keep a shell open and use a nested shell … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)