PowerShell Function Library

Most of my articles contain PowerShell scripts and most, if not all, of these scripts contain advanced functions. I have now consolidated all these functions in one PowerShell function library (*.PSM1).

 

 

Table of Contents

Introduction

This function library contains all advanced PowerShell functions used throughout my articles. This library comes in the form of a PowerShell module file (*.PSM1). This is one single file containing all functions.

All Windows related functions in the library have been created for Windows Server 2008 R2 / Windows 7 to Windows Server 2019 / Windows 10 1803.
All Citrix related functions have been created for Citrix Virtual Apps and Desktops (XenApp and XenDesktop) 7.13 and higher.

I created this PowerShell function library for the following reasons:

  • To provide one consolidated overview of all functions used throughout this blog.
  • To give you the opportunity to use my functions in your own custom scripts.
  • And last, but definitely not least, to be able to offer one consolidated PowerShell module, a *.psm1 file, that can be imported in a PowerShell script. This replaces the need to add each function directly to the PowerShell script. This offers the following advantages:
    • It dramatically reduces the total lines of code in a script
    • It removes the hassle of changing functions multiple times in multiple script.
    • When an existing function is updated or a new function is added, all you need to do is to replace the function library, the *.psm1 file, with the newer version. Next time your existing scripts run they automatically use the updated library.

In case you are missing a function, please send me an e-mail and I will see what I can do. In case you created a new function on your own, please send it to me so I can add it to the library. Your name and Twitter handle will be mentioned!

Download the PowerShell function library

PowerShell function library - PowerShell 5.0 logo

PowerShell function library v1.0.0

The latest version of the PowerShell function library can be downloaded here:

For a detailed overview of all changes please see the change log.

Documentation (detailed description of each function)

For a detailed description of each function, click on one of the links below to go to a specific category and locate your function there:

How to use the library in a PowerShell script

To use the function library in a script, proceed as follows:

  • Download the module file and copy it to a directory on your system, for example C:\Scripts.
  • Add the following line in your PowerShell script (*.PS1) to import the module:

The parent directory of the module file does not need to be specified when the parent directory has been added to the PSModulePath environment.

You can use the module in any custom PowerShell script; it does not have to be a script found on this website. Just make sure to add this line in the beginning of your script, at least before calling the first function. After importing the module file, all functions contained in the module can be called directly in your PowerShell script.

Note: I invite you to use my PowerShell scripting template.
Recommended: update to the latest version of PowerShell
For now, the functions included in the library require PowerShell version 3.0 at minimum. However, I strongly recommend you to install the latest version of PowerShell. Future functions may need a higher version of PowerShell. Currently, the latest version is 5.1 and is included in the Windows Management Framework 5.1 (Microsoft .NET Framework 4.5 or higher is required).

Make sure to check the section Logging.

How to use the library on the command line

You can use the PowerShell function library directly on the command line. Start a PowerShell command line and import the function library, for example like this:

Make sure to replace C:\Scripts for the actual directory where the module file is located.

All functions start with DS_. After importing the module file, you can scroll through all available functions by entering DS_ and clicking the tab key.

PowerShell function library - Scroll through functions using the TAB key

Any time you click the TAB key, another function is displayed.

PowerShell function library - Scroll through functions using the TAB key 2

All functions are advanced functions, which means that they include extensive logging. Please see the section Using the Get-Help command for more information.

When executing a function from the command line, all output is logged verbose (on screen within the console) as well as in a log file. For more details please see the section Logging.

 

 

Logging

If no log file (including log path) is specified, the PowerShell module automatically creates the folder C:\Logs and the log file DefaultLogFile_%Date%_%Time%.log.

All functions use the function DS_WriteLog for logging. The contents of a log file is quite detailed and all log files are formatted in the same way.

PowerShell function library - Extract log file

If you want to change the location of the log file when using the console, simply define the variable $LogFile before importing the module file, like this:

PowerShell function library - Define custom logfile

The same goes for PowerShell scripts (*.PS1):

PowerShell function library - Define custom logfile in script

Tips and tricks

Add the directory to the PSModulePath variable

In case the directory containing the module file is included in the environment variable PSModulePath, you do not need to specify the directory path when you execute the command Import-Module; only the file name of the module file (without the file extension *.PSM1) is required. For more information, see the section File Locations in my article Understanding PowerShell fundamentals.

Using the Get-Help command

All functions in the library are advanced functions. This means that they include detailed support information (help). When you load the module at the PowerShell command line you can use Get-Help %FunctionName% to see all help information, for example:
Get-Help DS_CreateDirectory

PowerShell function library - Import-Module and Get-Help

As shown in the screenshot below, you can view different levels of help information. Simply add one of the extensions (-examples, -detailed or -full) at the end of the line for more information, for example: Get-Help DS_CreateDirectory -examples

PowerShell function library - Import-Module and Get-Help examples

In case you run into any issues, please send me an e-mail. As always, happy scripting!