| by Arround The Web | No comments

Using Statement in PowerShell

PowerShell is a robust command-line shell and scripting language that is mostly used for controlling computer systems and automating administrative chores. The usage of statements is one of the most crucial PowerShell fundamentals. In particular, the “using” statement plays a crucial role in managing resources and ensuring efficient code execution.

This article will explore various aspects of the “using” statement in PowerShell, including its syntax, purpose, and practical applications.

Purpose of “using” Statement

The “using” statement lets you specify which namespaces are utilized in the session. By adding namespaces, you can import classes from script modules and assemblies and simplify the use of .NET classes and members.

MUSTs of “using” Statement

  • The “using” statement must appear before any other script or module statements. It cannot be preceded by any uncommented statements, including parameters.
  • Any variables must not be present in the “using” statement.
  • The “using” statement should not be confused with the “using:” scope modifier for variables. They both are different in their purpose and meaning.

Syntax of the “using” Statement

The syntax for the “using” statement is:

using namespace <.NET-namespace>

 

Let’s overview the following example:

using namespace System.IO

$Bytes = [File]::ReadAllBytes('D:\c sharp\Linuxhint1.txt')
[FileInfo]::new('D:\c sharp\Linuxhint1.txt')

 

In the code above, we first designated the namespace “System.IO”. The second line of the program, [File]::ReadAllBytes(‘D:c sharpLinuxhint1.txt’), reads every byte from the supplied file and places them in the $Bytes variable. In the third line, [FileInfo]::new(‘D:c sharpLinuxhint1.txt’) creates a new instance of the FileInfo class and returns the FileInfo object.

“using” Statement for Modules

We can also utilize the “using” statement for loading classes of a module.

Syntax

using module <module-name>

 

In this syntax, the “<module-name>”, a complete module specification, or a path to a module file can be used as the value for “module name”.

You can use either a fully qualified or relative path when “module-name>” is a path. When a “using” statement is present in a script, a relative path is resolved in that script.

PowerShell looks for the supplied module in the PSModulePath when “<module-name>” is a name or module specification. The following keys make up the hashtable that is a module specification:

ModuleName – Required. Names the module in question.

Optional GUID – specifies the module’s GUID.

Additionally, you must specify one of the three keys listed below.

ModuleVersion – A minimum allowable version of the module is specified via the “ModuleVersion” property.

MaximumVersion – Defines the module’s highest allowable version.

RequiredVersion – Determines the precise, necessary version of the module using “RequiredVersion”. The other Version keys cannot be used with this.

The root module (ModuleToProcess) of a binary module or script module is imported by the “using” module declaration. The classes specified in nested modules or scripts that are dot-sourced into the module are not reliably imported. Any classes that you want to be available to users outside of the module should be specified in the root module.

Here’s an example:

using module PSReadline

 

“using” Statement for Assembly

The “using” statement can also be used to preload types from a .NET assembly.

Syntax

using assembly <.NET-assembly-path>

 

In this syntax, when an assembly is loaded, the .NET types from that assembly are preloaded into the script before it is parsed. As a result, it is possible to develop fresh PowerShell classes that make use of the preloaded assembly types.

Look at an example of applying the “using” statement with an “assembly”:

using assembly System.Windows.Forms

 

In this command, we have loaded the assembly “System.Windows.Forms” in PowerShell using the “using” statement.

“using” statement for Hashtable Keys

Hash Tables” are adaptable data structures used in PowerShell for a variety of purposes, including storing configuration data, providing arguments to cmdlets, and storing data in scripts.

The cryptographic hash for the string “LinuxHint!” is obtained via the following script:

using namespace System.Text
using namespace System.IO[string]$string = "LinuxHint!"
[string]$algorithm = "SHA1"

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

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

 

The above PowerShell code begins by importing the namespaces required for file operations and encoding. The input string and the hashing algorithm (in this case “SHA1”), are then defined. The input string is then encoded using “Unicode” to create an array of bytes.

The content of the byte array is then used to build a “memorystream”. The hash value from the memorystream is calculated using the provided “SHA1” algorithm by the “Get-FileHash” cmdlet. The script ends by printing the obtained hash value as a string to the output.

Output

Conclusion

The “using” statement in PowerShell is a powerful tool for specifying namespaces, modules, or assemblies. Its syntax and purpose provide a standardized and safe approach for handling resources, facilitating proper disposal, and reducing code duplication.

Share Button

Source: linuxhint.com

Leave a Reply