PDB files are output when the C# compiler is compiling a .NET Assembly in Debug mode. I don't know why Add-Type
would be compiling with debug behavior by default, as this is not something I have noticed myself. However, if you want to explicitly suppress this behavior, you can specify compiler options, specifically the /debug-
(note the minus sign at the end), to the C# compiler.
In order to specify the compiler options, you must instantiate the System.CodeDom.Compiler.CompilerParameters
.NET class, specify the OutputAssembly
and CompilerOptions
properties on it, and then pass the CompilerParameters
object into the -CompilerParameters
parameter of the Add-Type
cmdlet.
Here is the MSDN documentation on the /debug
compiler parameter, and the documentation for the CompilerParameters
.NET class.
Note: You cannot use the -OutputAssembly
parameter on Add-Type
alongside the -CompilerParameters
parameter. Therefore, you will need to specify the OutputAssembly
property on the CompilerParameters
object, as previously discussed. The example code below indicates how to do this.
mkdir -Path c:\test;
$Code = @"
using System;
namespace test { };
"@
# 1. Create the compiler parameters
$CompilerParameters = New-Object -TypeName System.CodeDom.Compiler.CompilerParameters;
# 2. Set the compiler options
$CompilerParameters.CompilerOptions = '/debug-';
# 3. Set the output assembly path
$CompilerParameters.OutputAssembly = 'c:\test\Knuckle.dll';
# 4. Call Add-Type, and specify the -CompilerParameters parameter
Add-Type -CompilerParameters $CompilerParameters -TypeDefinition $Code;