Management

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 772 other subscribers

Social

  • LinkedIn
  • RSS Feed for Posts
  • Twitter
  • StumbleUpon

Working with Powershell and modules. [Solved]

For some time now I worked with Functions in Powershell, but wanted to go to the next level, working with modules.

Creating modules isn’t rocket science, but working with modules the right way is something completely different.

At first is I placed my modules at C:\Windows\system32\WindowsPowerShell\v1.0\Modules\

But later I found out that this location is reserved for Windows powershell, and the future updates.

Personal modules should be placed in the location reserved for the personal modules.

To find the personal location there is a special command:

$env:PSModulePath.split(‘;’)

This command show you, all of the locations where Powershell can use modules, without needing to load them prior to using them.

PS C:\Users\%Username%> $env:PSModulePath.split(‘;’)
C:\Users\%Username%\Documents\WindowsPowerShell\Modules
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\

The first one ‘C:\Users\%Username%\Documents\WindowsPowerShell\Modules’ is the private location you can use for your private modules

The second one ‘C:\Windows\system32\WindowsPowerShell\v1.0\Modules\’ is the default for powershell, and should NOT be used by you !

When using more Powershell options, there could be more locations where windows stores modules :

C:\Program Files\Common Files\Microsoft Lync Server 2013\Modules\
C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement

These are the location for Microsoft Lync server and Microsoft Azure

When you redirect your documents folder, your WindowsPowerShell\Modules is also redirected.

So always check the private location ( Default is your Documents\WindowsPowerShell\Modules folder ), and place your private modules at this location !

TIP : Try using ISESteroids it is awesome !
https://www.vspbreda.nl/nl/2015/04/extend-your-powershell-ise-with-tons-of-extra-features-use-isesteroids-2-0/

Example:

This scripts copies your local modules to your prive module folder.

<#
.SYNOPSIS
Copy modules to Scripts folder

.DESCRIPTION
This script copy's all the modules to your personal user module folder.

.INPUTS
Copy-Module.ps1

.OUTPUTS
None

.Prerequisites
Place your modules in the folder : $BaseDir\Scripts\Modules 

.EXAMPLE
Start copying the modules :
.\Copy-Module.ps1

.EXAMPLE
Start copying the modules and find more information :
.\Copy-Module.ps1 -Verbose

.LINK
http://www.vspbreda.nl

.NOTES
Written By:     Richard Voogt
Website:	http://www.vspbreda.nl
Twitter:	http://twitter.com/rvoogt

Change Log
V1.0, 21-6-2015 - First version

#>

[CmdletBinding()]
        param ()

$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$BaseDir = (new-object system.io.directoryinfo $ScriptDir).parent.parent.fullname

Write-Verbose 'Starting Verbose output'
Write-Verbose "Setting basic Directory $BaseDir"
Write-Verbose "ScriptsLoc = $BaseDir\Scripts"

function Set-Location
{
    [CmdletBinding()]
    param ($Location)
    if(!(Test-Path -Path $Location )){
        Write-Warning "Creating Module location $Location."
        New-Item -ItemType directory -Path $Location
    }
    ELSE {
        Write-Warning "Module location exists already exist in : $Location"
        }
}

Function Copy-Modules
{
    Write-Verbose "Copy From : $BaseDir\Scripts\Modules"
    Write-Verbose "Copy to : $script:PSModulePad"
    Copy-Item "$BaseDir\Scripts\Modules\*" $script:PSModulePad -recurse -Force
    Write-Output "The modules are copied to $script:PSModulePad"
}

function Get-ModulePath
{
    [CmdletBinding()]
    param ()
    
    $UserPath = $env:PSModulePath.split(';')
    $HomeFolder = [environment]::getfolderpath('mydocuments')
    ForEach ($Script:PSModulePad in $UserPath) { 
                                Write-Verbose "homefolder = $HomeFolder"
                                Write-Verbose "psmodulepad = $Script:PSModulePad "
                                If ($Script:PSModulePad -like "*$HomeFolder*"
                                    ){ 
                                    Write-Verbose "psmodulepad2 = $Script:PSModulePad "
                                    Set-Location $Script:PSModulePad
                                    Copy-Modules}
                                }
}

Get-ModulePath