# Add-Type

{% hint style="danger" %}
Using Add-Type to compile C# assembly, the source code will temporarily be written to disk.&#x20;
{% endhint %}

<figure><img src="/files/U3BSAsKpTYpdhHdlrewH" alt=""><figcaption></figcaption></figure>

## Example code:&#x20;

The [GetUserName](http://pinvoke.net/default.aspx/advapi32/GetUserName.html) function retrieves the name of the user associated with the current thread.

```powershell
powershell
$advapi32 = @'
    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool GetUserName(System.Text.StringBuilder sb, ref Int32 length);
'@

Add-Type -MemberDefinition $advapi32 -Namespace Advapi32 -Name Util

$size = 64
$str = New-Object System.Text.StringBuilder -ArgumentList $size

[Advapi32.util]::GetUserName($str,[ref]$size) | Out-Null
$str.ToString()
```

{% embed url="<https://www.pinvoke.net/default.aspx/advapi32.getusername>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rfc1918.gitbook.io/offsec/execution/powershell/c-assembly-in-powershell/add-type.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
