How to use dotNET (.NET) classes and methods in PowerShell 
Published: 2020-07-10 08:21:22

As a beginner in PowerShell, I wanted to explore how to use Microsoft .NET framework class library to run my PowerShell scripts. This information might be helpful if you’re going to use PowerShell at some point.

.NET languages are **object-oriented** with hierarchies of base and derived classes. A hierarchical namespace organizes **classes**. This allows you to locate a class in the .NET framework through a fully qualified name. For instance, the fully qualified name of the **Process** class is *System.Diagnostics.Process*.

Let us take a look at how this works.

In PowerShell, **Get-TimeZone** cmdlet gets the current time zone. Its equivalent .NET Framework class provides exactly the same output.
```powershell 
PS /> Get-TimeZone

Id                         : Singapore
DisplayName                : (UTC+08:00) Singapore Standard Time
StandardName               : Singapore Standard Time
DaylightName               : GMT+08:00
BaseUtcOffset              : 08:00:00
SupportsDaylightSavingTime : True
```
In order to find out the .NET class for the TimeZone, we use **Get-Member** cmdlet in PowerShell. The Get-Member cmdlet helps to identify what type of object the command returns, and also the properties and methods associated with that cmdlet (as shown below).
```powershell
PS /> Get-TimeZone | Get-Member


   TypeName: System.TimeZoneInfo

Name                       MemberType Definition
----                       ---------- ----------
Equals                     Method     bool Equals(System.TimeZoneInfo other), bool Equals(System.Object obj), bool IEquatable[TimeZoneI…
GetAdjustmentRules         Method     System.TimeZoneInfo+AdjustmentRule[] GetAdjustmentRules()
GetAmbiguousTimeOffsets    Method     timespan[] GetAmbiguousTimeOffsets(System.DateTimeOffset dateTimeOffset), timespan[] GetAmbiguous…
GetHashCode                Method     int GetHashCode()
```
Here **System.TimeZoneInfo** is the .NET class that displays the same output.
```powershell
PS /> [System.TimeZoneInfo]::local

Id                         : Singapore
DisplayName                : (UTC+08:00) Singapore Standard Time
StandardName               : Singapore Standard Time
DaylightName               : GMT+08:00
BaseUtcOffset              : 08:00:00
SupportsDaylightSavingTime : True
```
By using the GetType method we can verify that both commands return a System.TimeZoneInfo object. These two commands are shown here.
```powershell
PS /> (Get-TimeZone).gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     TimeZoneInfo                             System.Object

PS /> ([System.TimeZoneInfo]::local).GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     TimeZoneInfo                             System.Object

PS />
```
As we can see both these commands do exactly the same thing. However, I think PowerShell cmdlet is readable and convenient to use.

It is always a best practice, to use a native Windows PowerShell cmdlet when it exists unless there is a good reason for not doing so.
One such example is using a static method within a .NET class. To determine the power of a number we make use of System.Math class and use the method Pow
```powershell
PS /> [System.Math]::Pow(4,3)
64
PS /> [System.Math]::
E                 Atan              Ceiling           Exp               Log2              Round             Tanh
PI                Atan2             Clamp             Floor             Max               ScaleB            Truncate
Abs               Atanh             CopySign          FusedMultiplyAdd  MaxMagnitude      Sign
Acos              BigMul            Cos               IEEERemainder     Min               Sin
Acosh             BitDecrement      Cosh              ILogB             MinMagnitude      Sinh
Asin              BitIncrement      DivRem            Log               Pow               Sqrt
Asinh             Cbrt              Equals            Log10             ReferenceEquals   Tan
```
In this case, we are calling the .net framework static method of a class to do something that we couldn't do in PowerShell natively. Although, we could write a function in PowerShell, that uses this approach.