How can I re-use/import script code in PowerShell scripts?

Powershell

Powershell Problem Overview


I have to create a PowerShell script which does exactly same thing as my previous script, but this time I have to read a CSV file instead of an XML file. My plan is to create a PowerShell script which has common functions required for both scripts and re-use this common script file in both main files.

Suppose I create 2 main files in 2 directories in C:\ drive and keep my common file and other 3rd party libraries in a folder of D:\ drive, e.g. C:\script_1_folder\Script1.ps1, C:\script_2_folder\Script2.ps1 and common file and 3rd party libraries will be in D:\script_common.

  1. How do I call\re-use common file in my main files (how to get the path, do I have to create an instance of common file and how do I use it)

    What is the difference between

     $script_path    = $myinvocation.invocationname;      
     $script_folder  = split-path $script_path -parent;
     write-host  $script_folder  
     $script_name    = split-path $script_path -leaf;      
     $current_folder = [system.io.directory]::getcurrentdirectory()    
     [system.io.directory]::setcurrentdirectory($script_folder)
     Set-Location $script_folder
     add-type -path ".\AlexFTPS-1.1.0\AlexPilotti.FTPS.Client.dll"
    

    and

     $path = (split-path $MyInvocation.MyCommand.Path) 
     $loggerPath = $path + "\Logger\release\Logger.ps1";
     .$loggerPath; 
     $logger = Logger;   
     $logger.load($path + "\Logger\config\log4ps.xml","log.log"); 
    

    and what is the best way to do it with regard to my problem?

  2. How do I create a temp folder in windows temp folder?

Powershell Solutions


Solution 1 - Powershell

Common Code In Powershell

You can just put the code you want to include in a different PS1 file, and then "dot source" that file to include it in the current scope:

. D:\script_common\MyCode.ps1

That's all there is to that.

Using a Module

You might consider using a module instead, which can be included using the Import-Module cmdlet. You might have used this to work with things like Active Directory, where you could do something like this:

Import-Module ActiveDirectory

In that case, you only need the name of the module because it's in a special directory.

To write your own modules in Powershell, you name the module with a .psm1 extension. Typically, you don't do free floating code in one of these; you write functions which are then available to the code which imports the module.

To import a script module from anywhere, use the full path:

Import-Module D:\script_common\MyModule.psm1

Module Paths

When you create your own modules, you can keep them any old place and then refer to them by their full path (as above). There are also several locations that Powershell looks for modules:

  • $PSHome\Modules (%Windir%\System32\WindowsPowerShell\v1.0\Modules) -- Reserved for modules that ship with Windows. Do not put things here.
  • $Home\Documents\WindowsPowerShell\Modules (%UserProfile%\Documents\WindowsPowerShell\Modules)
  • %ProgramFiles%\WindowsPowerShell\Modules -- this isn't mentioned in the link, and seems to be used more for Desired State Configuration modules (probably because it applies to the entire system).

These are defaults, but Powershell uses its own environment variable called PSModulePath to determine where to look, and much like PATH you can add your own folder(s) to that variable.

That lets you keep your modules in your own location. Do see the link for more info on how to structure your folders and how to do naming.

So as far as keeping your modules and "3rd party" modules in the same place, that depends on the 3rd party stuff. It may install its own modules in its own place and modify the path, or it may just let you put them wherever you want.

Creating a Temp Folder

You can use the TEMP or TMP environment variables to get the path of the temp folder. To retrieve them in Powershell, use $env:TEMP or $env:TMP.

You'll have to come up with a unique name of a folder to create in there. One way to do that might be to use a GUID:

$dirName = [System.Guid]::NewGuid().ToString()
New-Item -Path "$($env:TEMP)\$dirName"

Solution 2 - Powershell

You should be able to dot source the script like that:

. "C:\script_common\script.ps1"

after that you can use all the functions like they were in the script you are running.

But... the better way to do it would be to create a module with your common functions (How to here: Scripting Guy´s Blog. (TLDR Version: place functions into psm1 file, put into modulepath, import using Import-Module, profit)

For creating a folder:

New-Item C:\Temp\yourfolder -type directory

Solution 3 - Powershell

Here is my attempt to create a template system in powershell : https://github.com/kayasax/PowershellTemplate
It allows to reuse functions you save in the repository by using tags in the template eg :
<include logging/log>
The content of the file log.ps1 found in the logging directory of the function repository will be inserted when tranforming template to script

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbluelabelView Question on Stackoverflow
Solution 1 - PowershellbriantistView Answer on Stackoverflow
Solution 2 - PowershellPaulView Answer on Stackoverflow
Solution 3 - PowershellLoïc MICHELView Answer on Stackoverflow