EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PowerShell Import Module

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » PowerShell Import Module

PowerShell Import Module

Introduction to PowerShell Import Module

A module is a collection of cmdlets, variables, functions, and workflows put together as a package. Modules were first introduced in PowerShell version 2. Modules are generally stored in the following two primary locations. In this topic, we are going to learn about PowerShell Import Module.

  • %windir%\system32\WindowsPowerShell\v1.0\Modules this is the location for system-wide modules available to any user in the system.
  • %USERPROFILE%\Documents\WindowsPowerShell\Modules

Each module has a dedicated folder in which it is saved. It also contains a psd1 file called Module Manifest. The manifest file contains the setting of the module like the version of PowerShell, author, and other settings. The list of available modules can get by running the Get-Module -ListAvailable cmdlet.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Importing a Module

Modules can be imported in a session by using the Import-Module cmdlet. For a module to be imported, it is necessary for it to be present on the system or on a remote server. Starting from PowerShell 3.0, Modules are automatically imported the first time any cmdlet in the installed module is run. $PSModuleAutoloadingPreference preference variable is used to enable or disable the automatic importing of modules. Import-Module cmdlets imports all the members by default, this can be restricted by using the alias or variable parameters.

Syntax of Import-Module

The following is the syntax of Import-Module Cmdlet

PowerShell Import-Module syntax 1

Parameters

There are many parameters that are associated with the Import-Module Cmdlet, few of the commonly used parameters will be discussed below.

1. Alias

This denotes the list of aliases that will be imported from the module into the current session. Some module automatically exports the selected alias by default. Its type is a string and the default value is none. It doesn’t accept pipeline input and wild card characters. It is an optional parameter.

2. Name

This is a mandatory parameter. This represents the names of the modules to be imported. The name can represent a module or a file inside the module such as a dll or .ps1 file. It’s wise always to use the module name. Its type is a string. The default value is none. It accepts pipeline input and wild card characters.

Popular Course in this category
Sale
All in One Data Science Bundle (360+ Courses, 50+ projects)360+ Online Courses | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (3,220 ratings)
Course Price

View Course

Related Courses
Shell Scripting Training (4 Courses, 1 Project)Data Visualization Training (15 Courses, 5+ Projects)

3.Prefix

This specifies the prefix that will be added to the names of the imported module members. This affects the module members in the current session. Its type is a string and the default value is none. It doesn’t accept pipeline input and wild card characters.

4. RequiredVersion

It denotes the version of the module that needs to be imported, in case if the specified version is not available then an error is thrown. Its type is version and the default value is none. It doesn’t accept pipeline input and wild card characters.

5. Scope

It denotes the scope in which the module must be imported. It can be either global or local. In global it is available to all commands in the session and in local it is only for the current scope. Its type is a string and default value is none. It doesn’t accept pipeline input and wild card characters.

6. Variable

It denotes the list of variables that needs to be imported from the module into the current session.  Its type is a string and default value is none. It doesn’t accept pipeline input, but wild card characters are accepted.

Examples

Here are the following examples of PowerShell Import Module mention below

Example #1

Importing a module and getting the commands in the module

Input:

Write-Host "Welcome to Import Module Tutorial" -ForegroundColor Green
Write-Host "List of modules available in the current session are below" -ForegroundColor Green
Get-Module -All
Write-Host "Importing Diagnostics Module" -ForegroundColor Green
Import-Module -Name PSDiagnostics
Write-Host "Commands available in PSDiagnostics are follows" -ForegroundColor Green
Get-Command -Module PSDiagnostics -TotalCount 1

Output:

PowerShell Import-Module output 1

PowerShell Import-Module output 1.2

Example #2

Importing a Module and Removing a Module along with the use of verbose:

The verbose parameter is used to list down the various scripts, functions, variables, and workflows that are available in the module being imported.

Input:

Write-Host "Welcome to verbose and removal of module tutorial" -ForegroundColor Green
Write-Host "Importing Diagnostics Module with verbose parameter" -ForegroundColor Green
Import-Module -Name PSDiagnostics -Verbose
Write-Host "Removing the module from the current session" -ForegroundColor Green
Remove-Module -Name PSDiagnostics
Write-Host "Successfully remove the Module"

Output:

output 2

Example #3

Restricting the members that are being imported:

As said above, it is possible to restrict the members of a module from being imported. A module can have 100 commands and functions. It’s not advisable to import all of them when only a few of them are going to be used. It’s better to import only the needed members than importing all. The following example shows how to restrict the members.

Input:

Write-Host "Welcome to the tutorial of restricting cmdlets from being imported" -ForegroundColor Green
Write-Host "The following are the available cmdlets in the PSDiagnostics Module" -ForegroundColor Green
(Get-Module PSDiagnostics).ExportedCommands
#mention only start and stop to be imported
Write-Host "Lets see how to import only start the trace and stop trace" -ForegroundColor Green
Import-Module PSDiagnostics -Function Start-Trace , Stop-Trace
Write-Host "only stop and start trace are imported" -ForegroundColor Green
Write-Host "imported cmdlets are as below" -ForegroundColor Green
Get-Command -Module PSDiagnostics

Output:

output 3

Example #4

Re-importing a module, adding a prefix and retrieving only a specified version of a module:

Input:

Write-Host "welcome to prefix tutorial" -ForegroundColor Green
Write-Host "Importing PSDiagnostics for first time" -ForegroundColor Green
Import-Module PSDiagnostics
Write-Host "Module Imported"
Write-Host "Importing PSDiagnostics for second  time with prefix" -ForegroundColor Green
Import-Module PSDiagnostics -Force -Prefix Px
Write-Host "List of commands with prefix px" -ForegroundColor Green
Get-Command -Module PSDiagnostics
Write-Host "Importing PowerShell get module with 3.0 version" -ForegroundColor Green
Import-Module -Name PowerShellGet -MinimumVersion 3.0.0
Write-Host "Module imported"

Output:

output 4

In the below the highlighted area shows the prefixed ones.

Conclusion – PowerShell Import Module

Thus, the article covered in detail about the Import-Module Cmdlet in detail. It also explained how a module can be imported, various ways of importing a module, filtering the commands and functions that need to be imported along with appropriate examples. To learn in-depth about the cmdlet it is advisable to create sample programs and have fun working around them.

Recommended Articles

This is a guide to PowerShell Import-Module. Here we discuss the examples of PowerShell Import-Module along with the Syntax and Parameters. You may also look at the following article.

  1. PowerShell Set-Content
  2. PowerShell Sort-Object
  3. Hashtable in PowerShell
  4. Complete Guide to PowerShell New-Item
  5. How to Implement PowerShell Rename-Item?
  6. Python Import Module | Examples
  7. PowerShell Convert to String | Parameters and Examples

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More