Package.psd1

About

This about file contains information about using hashtables and PSD1 files to configure Merge-Script. These psd1 files are also used by PowerShell Tools for Visual Studio Code.

Config File Schema

1
@{
2
Root = 'c:\Users\Adam\Desktop\service.ps1' # Root script to package. This is the main entry point for the package.
3
OutputPath = 'c:\Users\Adam\Desktop\out' # The output directory for the packaging process.
4
Package = @{
5
Enabled = $true # Whether to package as an executable.
6
Obfuscate = $false # Whether to obfuscate the resulting executable.
7
HideConsoleWindow = $false # Whether to hide the console window. Only valid for console applications.
8
# The target .NET Framework version. You will need the .NET Developer Pack for this version installed on your machine.
9
# If target PowerShell 7, you can also use netcoreapp31 here
10
DotNetVersion = 'v4.6.2'
11
FileVersion = '1.0.0' # The output file version
12
FileDescription = '' # The output file description
13
ProductName = '' # The output file product name
14
ProductVersion = '' # The output file product version.
15
Copyright = '' # The output file copyright
16
RequireElevation = $false # Whether to require elevation when running the executable. Only valid for console applications.
17
ApplicationIconPath = '' # The path to the application icon to use for the executable.
18
PackageType = 'Console' # The type of executable to generate. Valid values are Service or Console.
19
ServiceName = "" # The name of the service if the package type is Service.
20
ServiceDisplayName = "" # The display name of the service if the package type is Service.
21
HighDPISupport = $true # Whether to enable high DPI support for WinForm applications
22
PowerShellArguments = '' # Sets the arguments for the PowerShell process that is hosted within the executable. You can use arguments like -NoExit, -ExecutionPolicy and -NoProfile.
23
Platform = 'x64' # Sets the architecture of the executable. Can be either 'x86' or 'x64'
24
PowerShellVersion = 'Windows PowerShell' # You can specify Windows PowerShell or PowerShell 7 or later versions version (e.g. 7.0.0)
25
RuntimeIdentifier = 'win-x64' # You can specify other runtimes like linux-x64 (See .NET Core runtime identifiers)
26
DisableQuickEdit = $false # Disables the quick edit mode on windows console apps
27
Resources = [string[]]@() # Resources to embed in the output executable
28
}
29
Bundle = @{
30
Enabled = $true # Whether to bundle multiple PS1s into a single PS1. Always enabled when Package is enabled.
31
Modules = $true # Whether to bundle modules into the package
32
}
33
}
34
Copied!

Using a config file

A config file can be used either from within a PowerShell script as a hashtable or imported from a PSD1 file containing the hashtable.

Options

Root

The root script to package.

OutputPath

The path of the output directory for the resulting executable.

Package

Options for the packager. See the config file schema for the proper layout.

Enabled

Whether the packager is enabled. Valid values are either $true or $false.

Obfuscate

Whether to obfuscate the assembly. Only valid for Windows PowerShell. Valid values are $true or $false.

HideConsoleWindow

Whether to hide the console window. Useful for when packaging form applications. Valid values are $true or $false.

DotNetVersion

The .NET version to target for the executable. You can find the valid values below.
PowerShell Version
Valid .NET Versions
Windows PowerShell
net4.6.2, net4.7.0, net4.7.1, net4.7.2, net4.8.0
PowerShell 7.0.x
netcoreapp31
PowerShell 7.1.x
net5.0
PowerShell 7.2.x
net6.0

FileVersion

The file version to display in the assembly properties.

FileDescription

The file description to display in the assembly properties.

ProductName

The product name to display in the assembly properties.

ProductVersion

The product version to display in the assembly properties.
The copyright to display in the assembly properties.

RequireElevation

Whether the executable requires elevation to run. This setting is only supported on Windows. Either $true or $false.

ApplicationIconPath

The path to the icon to display for this application.

PackageType

The type of package to product. Valid values are Console or Service.

ServiceName

The name of the service when packaging a service.

ServiceDisplayName

The display name of the service when packaging a service.

HighDPISupport

Enable high DPI support for Windows Forms applications. Either $true or $false.

PowerShellArguments

Additional arugments to provide to the PowerShell process. This can include arguments like -ExecutionPolicy or -NoProfile. Do not include -Command.

Platform

The target architecture for the executable. This should be either x86 or x64.

PowerShellVersion

The PowerShell version to target. Ensure that you specify a supported .NET version when selecting your PowerShell version. Supported versions are:
  • Windows PowerShell
  • 7.0.0
  • 7.0.1
  • 7.0.2
  • 7.0.3
  • 7.0.6
  • 7.1.0
  • 7.1.1
  • 7.1.2
  • 7.1.3
  • 7.1.4
  • 7.1.5
  • 7.2.0

RuntimeIdentifier

The .NET runtime identifier to target. This defaults to win-x64. If you wish to target Linux, you could specify linux-x64. You can find a list of valid .NET runtime identifiers here.

DisableQuickEdit

Disables the quick edit mode on Windows console applications. This defaults to $false. Either $true or $false.

Resources

An array of resources to include with the executable. This should be an array of strings. These resources will be stored as embedded resources.

DotNetSdk

This is an advanced option. The target .NET SDK to use when packaging the executable. If not specified, the highest version will be used.

Certificate

The certificate used to sign the assembly. The packager will use Set-AuthenticodeSignature to sign the assembly. This should be the path to a valid code signing certificate. For example: 'Cert:\CurrentUser\AuthRoot\02FAF3E291435468607857694DF5E45B68851555'

OutputName

The name of the output assembly. When this is not specified, this will be the root script name.

Bundle

Enabled

Whether bundling is enabled. Bundling will include referenced scripts and modules in the resulting executable.

Modules

Whether to bundle modules with the script executable. Modules will only be bundled when imported with Import-Module.

NestedModules

Whether to include nested modules of packaged modules. Requires Modules to be set to $true.

IgnoredModules

A list of modules to ignore during packaging. This should be an array of strings.

EXAMPLES

It is not required to include all aspects of the config when using Merge-Script. The only required components are Root and OutputPath. Aside from that, anything that is not include will be considered false. This means that in the below example, packaging is disabled but bundling is not. The below operation will not bundle nested modules or required assemblies of any modules it is bundling.
1
Merge-Script -Config @{
2
Root = ".\MyScript.ps1"
3
OutputPath = ".\"
4
Bundle = @{
5
Enabled = $true
6
Modules = $true
7
}
8
}
Copied!

Create console application

Creates a PowerShell console based application that has an application icon and hides the console window.
1
@{
2
Root = 'c:\Users\Adam\Desktop\form.ps1'
3
OutputPath = 'c:\Users\Adam\Desktop\out'
4
Package = @{
5
Enabled = $true
6
HideConsoleWindow = $true
7
DotNetVersion = 'v4.6.2'
8
ApplicationIconPath = 'C:\users\adam\desktop\icon.ico'
9
}
10
}
11
Copied!

Create a service

Creates a PowerShell service based on the service.ps1 file and outputs to the out directory on the desktop. It will use the .NET 4.6.2 Developer Pack. The service name will be PSService and the display name will be PowerShell Service.
For more information on services, see the Package as Service section.
1
@{
2
Root = 'c:\Users\Adam\Desktop\service.ps1'
3
OutputPath = 'c:\Users\Adam\Desktop\out'
4
Package = @{
5
Enabled = $true
6
DotNetVersion = 'v4.6.2'
7
FileVersion = '1.0.0'
8
FileDescription = ''
9
ProductName = ''
10
ProductVersion = ''
11
Copyright = ''
12
PackageType = 'Service'
13
ServiceName = "PSService"
14
ServiceDisplayName = "PowerShell Service"
15
}
16
}
17
Copied!
After building a service, you can install the service with the --install parameter of your service's executable. To uninstall a service, use the --uninstall parameter.

Package PowerShell 7.0

Creates an executable that contains the PowerShell 7.0 engine. This executable does not require the target machine have PowerShell or .NET Core installed. The size of the executable will be considerably larger than a typical Merge-Script executable.
1
@{
2
Root = 'c:\Users\Adam\Desktop\script.ps1'
3
OutputPath = 'c:\Users\Adam\Desktop\out'
4
Package = @{
5
Enabled = $true
6
DotNetVersion = 'netcoreapp3.1'
7
PowerShellVersion = "7.0.0"
8
}
9
Bundle = @{
10
Enabled = $true
11
Modules = $true
12
}
13
}
Copied!

Package PowerShell 7.1

You can package PowerShell 7.1 scripts by targeting .NET 5.0. You will need the .NET 5.0 SDK or later.
1
@{
2
Root = 'c:\Users\Adam\Desktop\script.ps1'
3
OutputPath = 'c:\Users\Adam\Desktop\out'
4
Package = @{
5
Enabled = $true
6
DotNetVersion = 'net5.0'
7
PowerShellVersion = "7.1.0"
8
}
9
Bundle = @{
10
Enabled = $true
11
Modules = $true
12
}
13
}
Copied!

Package PowerShell 7.2

PowerShell Pro Tools 2021.12.0 or later required.
You can package PowerShell 7.2 scripts by targeting .NET 6.0. You will need the .NET 6.0 SDK or later.
1
@{
2
Root = 'c:\Users\Adam\Desktop\script.ps1'
3
OutputPath = 'c:\Users\Adam\Desktop\out'
4
Package = @{
5
Enabled = $true
6
DotNetVersion = 'net6.0'
7
PowerShellVersion = "7.2.0"
8
}
9
Bundle = @{
10
Enabled = $true
11
Modules = $true
12
}
13
}
Copied!

Bundle resources in a WPF application

Embeds the image.png file within the application so you can reference it in your XAML. This file resides in the same folder as window.ps1.
1
@{
2
Root = 'c:\Users\Adam\Desktop\Window.ps1'
3
OutputPath = 'c:\Users\Adam\Desktop\out'
4
Package = @{
5
Enabled = $true
6
Resources = [string[]]@("image.png")
7
}
8
Bundle = @{
9
Enabled = $true
10
Modules = $true
11
}
12
}
Copied!
In the XAML, you can reference the image like this.
1
<Window x:Class="WpfApp1.MainWindow"
2
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6
xmlns:local="clr-namespace:WpfApp1"
7
mc:Ignorable="d"
8
Title="MainWindow" Height="450" Width="800">
9
<Grid>
10
<Image Source="image.png" />
11
</Grid>
12
</Window>
Copied!

Access Resources in Your Script

You can access resources in your script using the following function.
1
function Get-Resource {
2
param($Name)
3
4
$ProcessName = (Get-Process -Id $PID).Name
5
$Stream = [System.Reflection.Assembly]::GetEntryAssembly().GetManifestResourceStream("$ProcessName.g.resources")
6
$KV = [System.Resources.ResourceReader]::new($Stream) | Where-Object Key -EQ $Name
7
[System.IO.StreamReader]::new($KV.Value).ReadToEnd()
8
}
Copied!
In your script, just use this function to load the file.
1
$MyManifest = Get-Resource -Name 'manifest.json'
Copied!
You will package the resource file, just like you do with WPF applications.
1
@{
2
Root = 'c:\Users\adamr\Desktop\variables.ps1'
3
OutputPath = 'c:\Users\adamr\Desktop\out'
4
Package = @{
5
Enabled = $true
6
Resources = [string[]]@("manifest.json")
7
}
8
Bundle = @{
9
Enabled = $true
10
Modules = $true
11
}
12
}
Copied!