CmdID

CmdID / PowerShell / Cmdlets / Add

Add-Type

Adds a Microsoft .NET class to a PowerShell session. PowerShell reference

From the PowerShell reference 16 parameters

Opens CmdID with your command, read as PowerShell. It is read in your browser and sent nowhere.

What it does

from the PowerShell reference, CC BY 4.0

Adds a Microsoft .NET class to a PowerShell session. The Add-Type cmdlet lets you define a Microsoft .NET Framework class in your PowerShell session. You can then instantiate objects, by using the New-Object cmdlet, and use the objects just as you would use any .NET Framework object. If you add an Add-Type command to your PowerShell profile, the class is available in all PowerShell sessions. You can specify the type by specifying an existing assembly or source code files, or you can specify the source code inline or saved in a variable. You can even specify only a method and Add-Type defines and generates the class. On Windows, you can use this feature to make Platform Invoke (P/Invoke) calls to unmanaged functions in PowerShell. If you specify source code, Add-Type compiles the specified source code and generates an in-memory assembly that contains the new .NET Framework types. You can use the parameters of Add-Type to specify an alternate language and compiler, C# is the default, compiler options, assembly dependencies, the class namespace, the names of the type, and the resulting assembly.

Add-Type [-CodeDomProvider <CodeDomProvider>] [-CompilerParameters <CompilerParameters>] [-TypeDefinition] <String> [-Language <Language>] [-ReferencedAssemblies <String[]>] [-OutputAssembly <String>] [-OutputType <OutputAssemblyType>] [-PassThru] [-IgnoreWarnings] [<CommonParameters>]
Add-Type [-CodeDomProvider <CodeDomProvider>] [-CompilerParameters <CompilerParameters>] [-Name] <String> [-MemberDefinition] <String[]> [-Namespace <String>] [-UsingNamespace <String[]>] [-Language <Language>] [-ReferencedAssemblies <String[]>] [-OutputAssembly <String>] [-OutputType <OutputAssemblyType>] [-PassThru] [-IgnoreWarnings] [<CommonParameters>]
Add-Type [-CompilerParameters <CompilerParameters>] [-Path] <String[]> [-ReferencedAssemblies <String[]>] [-OutputAssembly <String>] [-OutputType <OutputAssemblyType>] [-PassThru] [-IgnoreWarnings] [<CommonParameters>]
Add-Type [-CompilerParameters <CompilerParameters>] -LiteralPath <String[]> [-ReferencedAssemblies <String[]>] [-OutputAssembly <String>] [-OutputType <OutputAssemblyType>] [-PassThru] [-IgnoreWarnings] [<CommonParameters>]
Add-Type -AssemblyName <String[]> [-PassThru] [-IgnoreWarnings] [<CommonParameters>]

Add a .NET type to a session

$Source = @" public class BasicTest { public static int Add(int a, int b) { return (a + b); } public int Multiply(int a, int b) { return (a * b); } } "@ Add-Type -TypeDefinition $Source [BasicTest]::Add(4, 3) $BasicTestObject = New-Object BasicTest $BasicTestObject.Multiply(5, 2)Identify

Examine an added type

[BasicTest] | Get-MemberIdentify

Add types from an assembly

$AccType = Add-Type -AssemblyName "accessib*" -PassThruIdentify

Call native Windows APIs

$Signature = @" [DllImport("user32.dll")]public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); "@ $addTypeSplat = @{ MemberDefinition = $Signature Name = "Win32ShowWindowAsync" Namespace = 'Win32Functions' PassThru = $true } $ShowWindowAsync = Add-Type @addTypeSplat # Minimize the PowerShell console $ShowWindowAsync::ShowWindowAsync((Get-Process -Id $PID).MainWindowHandle, 2) # Restore the PowerShell console $ShowWindowAsync::ShowWindowAsync((Get-Process -Id $PID).MainWindowHandle, 4)Identify

Add a type from a Visual Basic file

Add-Type -Path "C:\PS-Test\Hello.vb" [VBFromFile]::SayHello(", World") # From Hello.vb Public Class VBFromFile Public Shared Function SayHello(sourceName As String) As String Dim myValue As String = "Hello" return myValue + sourceName End Function End ClassIdentify

Add a class with JScript.NET

Add-Type @' class FRectangle { var Length : double; var Height : double; function Perimeter() : double { return (Length + Height) * 2; } function Area() : double { return Length * Height; } } '@ -Language JScript $rect = [FRectangle]::new() $rectIdentify

Add an F# compiler

Add-Type -Path "FSharp.Compiler.CodeDom.dll" $Provider = New-Object Microsoft.FSharp.Compiler.CodeDom.FSharpCodeProvider $FSharpCode = @" let rec loop n =if n <= 0 then () else beginprint_endline (string_of_int n);loop (n-1)end "@ $FSharpType = Add-Type -TypeDefinition $FSharpCode -CodeDomProvider $Provider -PassThru | Where-Object { $_.IsPublic } $FSharpType::loop(4)Identify

Its page in the reference: Add-Type.

Parameters

16 in the PowerShell reference
-AssemblyNameSpecifies the name of an assembly that includes the types. Add-Type takes the types from the specified assembly. This parameter is required when you're creating types based on an assembly name. Enter the full or simple name, also known as the partial name, of an assembly. Wildcard characters are permitted in the assembly name. If you enter a simple or partial name, Add-Type resolves it to the full name, and then uses the full name to load the assembly. Using the Path or LiteralPath parameters guarantees that you are loading the assembly that you intended to load. When you use the AssemblyName parameter, PowerShell asks .NET to resolve the assembly name using the standard .NET assembly resolution process. Since .NET searches the application folder first, Add-Type might load an assembly from $PSHOME instead of the version in the current folder. For more information, see Assembly location. If .NET fails to resolve the name, PowerShell then looks in the current location to find the assembly. When you use wildcards in the AssemblyName parameter, the .NET assembly resolution process fails causing PowerShell to look in the current location.
-CodeDomProviderSpecifies a code generator or compiler. Add-Type uses the specified compiler to compile the source code. The default is the C# compiler. Use this parameter if you're using a language that can't be specified by using the Language parameter. The CodeDomProvider that you specify must be able to generate assemblies from source code.
-CompilerParametersSpecifies the options for the source code compiler. These options are sent to the compiler without revision. This parameter allows you to direct the compiler to generate an executable file, embed resources, or set command-line options, such as the /unsafe option. This parameter implements the CompilerParameters class, System.CodeDom.Compiler.CompilerParameters. You can't use the CompilerParameters and ReferencedAssemblies parameters in the same command.
-IgnoreWarningsIgnores compiler warnings. Use this parameter to prevent Add-Type from handling compiler warnings as errors.
-LanguageSpecifies the language that is used in the source code. The Add-Type cmdlet uses the value of this parameter to select the appropriate CodeDomProvider. CSharp is the default value. The acceptable values for this parameter are as follows: - CSharp - CSharpVersion2 - CSharpVersion3 - JScript - VisualBasic
-LiteralPathSpecifies the path to source code files or assembly DLL files that contain the types. Unlike Path, the value of the LiteralPath parameter is used exactly as it's typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell PowerShell not to interpret any characters as escape sequences. Using the Path or LiteralPath parameters guarantees that you are loading the assembly that you intended to load.
-MemberDefinitionSpecifies new properties or methods for the class. Add-Type generates the template code that's required to support the properties or methods. On Windows, you can use this feature to make Platform Invoke (P/Invoke) calls to unmanaged functions in PowerShell.
-NameSpecifies the name of the class to create. This parameter is required when generating a type from a member definition. The type name and namespace must be unique within a session. You can't unload a type or change it. To change the code for a type, you must change the name or start a new PowerShell session. Otherwise, the command fails.
-NamespaceBy default, this command creates the type in the Microsoft.PowerShell.Commands.AddType.AutoGeneratedTypes namespace. When you use this parameter, the type is created in the specified namespace. If the value an empty string, the type is created in the global namespace.
-OutputAssemblyGenerates a DLL file for the assembly with the specified name in the location. Enter an optional path and filename. Wildcard characters are permitted. By default, Add-Type generates the assembly only in memory. If you output the assembly to a file you should include the PassThru parameter to return the type from the newly created assembly.
-OutputTypeSpecifies the output type of the output assembly. By default, no output type is specified. This parameter is valid only when an output assembly is specified in the command. For more information about the values, see OutputAssemblyType Enumeration. The acceptable values for this parameter are as follows: - ConsoleApplication - Library - WindowsApplication
-PassThruReturns a System.Runtime object that represents the types that were added. By default, this cmdlet doesn't generate any output. Use this parameter if you used OutputAssembly to create a DLL file and you want to return the type from the newly created assembly.
-PathSpecifies the path to source code files or assembly DLL files that contain the types. If you submit source code files, Add-Type compiles the code in the files and creates an in-memory assembly of the types. The file extension specified in the value of Path determines the compiler that Add-Type uses. If you submit an assembly file, Add-Type takes the types from the assembly. To specify an in-memory assembly or the global assembly cache, use the AssemblyName parameter.
-ReferencedAssembliesSpecifies the assemblies upon which the type depends. By default, Add-Type references System.dll and System.Management.Automation.dll. The assemblies that you specify by using this parameter are referenced in addition to the default assemblies. You can't use the CompilerParameters and ReferencedAssemblies parameters in the same command.
-TypeDefinitionSpecifies 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. For more information about here-strings, see about_Quoting_Rules. 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.
-UsingNamespaceSpecifies other namespaces that are required for the class. This is much like the C# keyword, Using. By default, Add-Type references the System namespace. When the MemberDefinition parameter is used, Add-Type also references the System.Runtime.InteropServices namespace by default. The namespaces that you add by using the UsingNamespace parameter are referenced in addition to the default namespaces.