Add-Type

Add a .NET Framework type to a PowerShell session. If a .NET Framework class is added to your PowerShell session with Add-Type, those objects may then be instantiated (with New-Object ), just like any .NET Framework object.

Syntax
      Add-Type -AssemblyName string[]
         [-IgnoreWarnings] [-PassThru] [CommonParameters]
    
      Add-Type [-Name] string [-MemberDefinition] string[]
         [-CodeDomProvider CodeDomProvider] [-CompilerParameters CompilerParameters]
            [-Language {CSharp | CSharpVersion3 | VisualBasic | JScript}]
               [-Namespace string] [-OutputAssembly string]
                  [-OutputType OutputAssemblyType] [-ReferencedAssemblies string[]]
                     [-UsingNamespace string[]] [-IgnoreWarnings] [-PassThru] [CommonParameters]
    
      Add-Type [-Path] string[] [-CompilerParameters CompilerParameters]
        [-OutputAssembly string]
           [-OutputType OutputAssemblyType] [-ReferencedAssemblies string[]]
              [-IgnoreWarnings] [-PassThru] [CommonParameters]
    
      Add-Type [-TypeDefinition] string [-CodeDomProvider CodeDomProvider]
         [-CompilerParameters CompilerParameters]
            [-Language {CSharp | CSharpVersion3 | VisualBasic | JScript}]
               [-OutputAssembly string] [-OutputType OutputAssemblyType]
                  [-ReferencedAssemblies string[]] [-IgnoreWarnings] 
                     [-PassThru] [CommonParameters]

       Add-Type [-CompilerParameters CompilerParameters] [-OutputAssembly String]
          [-OutputType {ConsoleApplication | Library | WindowsApplication}]
             [-ReferencedAssemblies String[]] -LiteralPath String[]
                [-IgnoreWarnings] [-PassThru] [CommonParameters]

Key
   -AssemblyName 
       The name of an assembly that includes the types. Wildcard characters are permitted
       This parameter does not accept a path or file name. To enter the path to the assembly DLL, use -Path.

   -CodeDomProvider
       A code generator or compiler. The default is the CSharp compiler.

   -CompilerParameters
       The options for the source code compiler.
       You cannot use -CompilerParameters and -ReferencedAssemblies in the same command.
       These options are sent to the compiler without revision.
       
   -IgnoreWarnings
       Ignores compiler warnings. Don’t handle compiler warnings as errors.

   -Language
       Specifies the language used in the source code.
       Valid values are "CSharp", "CSharpVersion3", "VisualBasic", and "JScript".
       "CSharp" is the default.

   -LiteralPath String[]
       The path to source code files or assembly DLL files that contain the types.
       Unlike -Path, the value of -LiteralPath is used exactly as it is typed.
       No characters are interpreted as wildcards. If the path includes escape characters,
       enclose it in single quotation marks, this tells PowerShell not to interpret any
       characters as escape sequences.

   -MemberDefinition String[]
       Specify new properties or methods for the class. Add-Type generates the template code
       required to support the properties or methods.
       Use this feature to make Platform Invoke (P/Invoke) calls to unmanaged functions in PowerShell.

   -Name String
       The name of the class to create. Required when generating a type from a member definition.
       The type name and namespace must be unique within a session. You cannot unload a type or
       change it.
       If you need to change the code for a type, change the name or start a new PowerShell session.
       Otherwise, the command will fail.

   -Namespace String
       A namespace for the type. If this parameter is not included in the command, the type is
       created in the   Microsoft.PowerShell.Commands.AddType.AutoGeneratedTypes  namespace.
       If the parameter is included in the command with an empty string value or a value of $null,
       the type is generated in the global namespace.

   -OutputAssembly String
       Generate a DLL file for the assembly with the specified name in the location. Enter a path (optional)
       and file name. Wildcard characters are permitted. By default, Add-Type generates the assembly only in memory.

   -OutputType OutputAssemblyType
       The output type of the output assembly. 
       Valid values are Library, ConsoleApplication, and WindowsApplication.
       By default, no output type is specified.

   -PassThru
       Return a System.Runtime object that represents the types that were added.

   -Path
       The path to source code files or assembly DLL files that contain the types.

   -ReferencedAssemblies
       The assemblies upon which the type depends in addition to the default references,
       System.dll and System.Management.Automation.dll
       You cannot use -CompilerParameters and -ReferencedAssemblies in the same command.

   -TypeDefinition
       The source code that contains the type definitions. Enter the source code in a
       string or here-string, or enter a variable that contains the source code.

       Include a namespace declaration in your type definition. If you omit the namespace declaration,
       your type might have the same name as another type or the shortcut for another type,
       causing an unintentional overwrite. For example, if you define a type called "Exception",
       scripts that use "Exception" as the shortcut for System.Exception will fail.

   -UsingNamespace
       Other namespaces that are required for the class.
       This is much like the Using keyword in C#.

Examples

Adds the classes from the Accessibility assembly to the current session:

PS C:\> $accType = Add-Type -AssemblyName accessib* -PassThru

The command uses the -AssemblyName parameter to specify the name of the assembly. The wildcard character allows you to get the correct assembly even when you are not sure of the name or its spelling.

The command uses the -PassThru parameter to generate objects that represent the classes that are added to the session, and it saves the objects in the $accType variable.

For more see: get-help Add-Type -examples

“If you don’t have time to do it right, when will you have time to do it over?” ~ John Wooden

Related PowerShell Cmdlets

Enum - Create and use PowerShell enums.


 
Copyright © 1999-2024 SS64.com
Some rights reserved