Short description

Allows you to specify namespaces to use in the session.

Long description

The using statement allows you to specify namespaces to use in the session. Adding namespaces simplifies usage of .NET classes and members and allows you to import classes from script modules and assemblies.

The using statements must come before any other statements in a script or module. No uncommented statement can precede it, including parameters.

The using statement must not contain any variables.

The using statement isn't the same as the using: scope modifier for variables. For more information, see about_Remote_Variables.

Namespace syntax

To resolve types from a .NET namespace:

using namespace <.NET-namespace>

Specifying a namespace makes it easier to reference types by their short names.

Module syntax

To load classes and enumerations from a PowerShell module:

using module <module-name>

The value of <module-name> can be a module name, a full module specification, or a path to a module file.

When <module-name> is a path, the path can be fully qualified or relative. A relative path resolves relative to the script that has the using statement.

When <module-name> is a name or module specification, PowerShell searches the PSModulePath for the specified module.

A module specification is a hashtable that has the following keys.

  • ModuleName - Required Specifies the module name.
  • GUID - Optional Specifies the GUID of the module.
  • It's also Required to specify at least one of the three below keys.
    • ModuleVersion - Specifies a minimum acceptable version of the module.
    • MaximumVersion - Specifies the maximum acceptable version of the module.
    • RequiredVersion - Specifies an exact, required version of the module. This can't be used with the other Version keys.

Import-Module and the #requires statement only import the module functions, aliases, and variables, as defined by the module. Classes and enumerations aren't imported.

The using module statement imports classes and enumerations from the root module (ModuleToProcess) of a script module or binary module. It doesn't consistently import classes or enumerations defined in nested modules or in scripts that are dot-sourced into the root module. Define classes and enumerations that you want to be available to users outside of the module directly in the root module.

During development of a script module, it's common to make changes to the code then load the new version of the module using Import-Module with the Force parameter. This works for changes to functions in the root module only. Import-Module doesn't reload any nested modules. Also, there's no way to load any updated classes or enumerations.

To ensure that you're running the latest version, you must start a new session. Classes and enumerations defined in PowerShell and imported with a using statement can't be unloaded.

Assembly syntax

To preload types from a .NET assembly:

using assembly <.NET-assembly-path>

Loading an assembly preloads .NET types from that assembly into a script at parse time. This allows you to create new PowerShell classes that use types from the preloaded assembly.

If you aren't creating new PowerShell classes, use the Add-Type cmdlet instead. For more information, see Add-Type.


Example 1 - Add namespaces for typename resolution

The following script gets the cryptographic hash for the "Hello World" string.

Note how the using namespace System.Text and using namespace System.IO simplify the references to [UnicodeEncoding] in System.Text and [Stream] and [MemoryStream] in System.IO.

using namespace System.Text
using namespace System.IO

[string]$string = "Hello World"
## Valid values are "SHA1", "SHA256", "SHA384", "SHA512", "MD5"
[string]$algorithm = "SHA256"

[byte[]]$stringbytes = [UnicodeEncoding]::Unicode.GetBytes($string)

[Stream]$memorystream = [MemoryStream]::new($stringbytes)
$hashfromstream = Get-FileHash -InputStream $memorystream `
  -Algorithm $algorithm

Example 2 - Load classes from a script module

In this example, a PowerShell script module named CardGames defines the following classes:

  • Deck
  • Card

Import-Module and the #requires statement only import the module functions, aliases, and variables, as defined by the module. Classes aren't imported. The using module command imports the module and also loads the class definitions.

using module CardGames

[Deck]$deck = [Deck]::new()
[Card[]]$hand1 = $deck.Deal(5)
[Card[]]$hand2 = $deck.Deal(5)
[Card[]]$hand3 = $deck.Deal(5)

Example 3 - Load classes from an assembly

This example loads an assembly so that its classes can be used when processing data. The following script converts data into a YAML format.

using assembly './YamlDotNet.dll'
using namespace YamlDotNet

$yamlSerializer = [Serialization.Serializer]::new()

$info = [ordered]@{
  Inventory = @(
    @{ Name = 'Apples' ; Count = 1234 }
    @{ Name = 'Bagels' ; Count = 5678 }
  CheckedAt = [datetime]'2023-01-01T01:01:01'

- Name: Apples
  Count: 1234
- Name: Bagels
  Count: 5678
CheckedAt: 2023-01-01T01:01:01.0000000