Learn About the PowerShell 5.0 Using Statement

Sign in to queue

Description

Check out the cloud & automation training courses that are developed by Art of Shell, who sponsored this video!

In PowerShell 5.0, the using statement enables two new, exciting scenarios! If you're using Windows 10, you already have PowerShell 5.0 available to you! Support for PowerShell 5.0 is also offered via the Windows Management Framework (WMF) package, for down-level operating systems including Windows 7, Windows 8, and Windows Server 2012 R2.

First of all, you can "import" .NET namespaces, similar to the C# language, so you don't have to type the full path to a .NET type. For example, instead of typing [System.Data.SqlClient.SqlConnection] every time you want to reference the SqlConnection .NET class, you can add a using statement for the System.Data.SqlClient .NET namespace. Once you've done this, you can simply reference the class like so: [SqlConnection].

The second scenario that the using statement enables in PowerShell is the ability to import PowerShell Classes from PowerShell modules! If you've created a module that defines one or more PowerShell Classes, you can import those classes from the module by adding a using statement to your PowerShell scripts. The proper syntax for this is: using module <moduleName>. Once you've "used" the module, you can then access the class definitions inside of it!

Watch this video for a demonstration about both techniques!

Cheers,
Trevor Sullivan
Microsoft MVP: Cloud & Data Center Management
Founder, Art of Shell
E-mail: trevor@artofshell.com

Embed

Download

The Discussion

  • User profile image
    robbiepc30

    Great Video!

  • User profile image
    s3curity​Consult

    Thanks for the tip

  • User profile image
    postanote

    QQ.

    Good Vid, but I have been looking around at the $psISE.Options. I can set PoSH console host and the console screen to whatever color thing I need. But I can't seem the discover how you set your ISE Tab color scheme to what you display.

    I use dark themes most of the time, VSCode, VS etc., but I was just wondering where that options is for the ISE tab. 

    But one could also assume this is only in the ISE Insider build???

    vs the RTM v5 one.

     

    TYIA

  • User profile image
    postanote

    Never mind.
    I figured it out... 8^)

  • User profile image
    Edwin Ashdown

    Great video, really cool features that i now know more about.

  • User profile image
    Dmitry​Lobanov

    How can we "reload" a class which have been imported from some module using "using module module.psm1"? In case we're coding new logic and improving classes in module.psm1, we test it in ISE, find issues, fix code and then want to reload class using "using module module.psm1"... but it seems to not work. 

Add Your 2 Cents