<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1678611822423757&amp;ev=PageView&amp;noscript=1">
Defrag This

| Read. Reflect. Reboot.

How to Publish Scripts to the PowerShell Gallery

Adam Bertram| January 08 2019

| IT insights, PowerShell, Scripting

how-to-publish-scripts-to-the-powershell-gallery

Creating excellent PowerShell scripts is one thing, but sharing them with others takes them to a whole new level! One of the best ways to improve your PowerShell skills, share your scripts with the community, and make your work known is by publishing scripts to the PowerShell Gallery.

The PowerShell Gallery is usually known for publishing PowerShell modules and software packages, but we can also publish ol’ fashioned PS1 scripts there as well.

Anyone can publish content to the PowerShell Gallery, but you first need to set up an account. Once your account is set up, you’ll then need the unique API key that’s assigned to you. You can find your API key by going to your profile in the PowerShell Gallery.

PowerShell Gallery API KeyPowerShell Gallery API Key

Once you know your API key, you’re ready to go! We first need a script to publish. I’ll create a stupidly simple script just so we have a PS1 file to upload as shown below. I’ll call this script adbtest.ps1.

param(
    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string]$Name
)

Write-Host $Name

Once I’ve figured out the script I’d like to publish, I then need to call the Publish-Script cmdlet. At it’s simplest, this cmdlet requires two parameters; the path to the script and your API key defined as the NuGetApiKey parameter.

Learn how to automate IT tasks with PowerShell. Download this eBook. 

If I try to publish this script as-is though, we soon run into a problem.

PS> Publish-Script -Path C:\adbtest.ps1 -NuGetApiKey <ApiKey>

NuGet.exe is required to continue
PowerShellGet requires NuGet.exe to publish an item to the NuGet-based repositories. NuGet.exe must be available in 'C:\ProgramData\Microsoft\Windows\PowerShell\PowerShellGet\' or 'C:\Users\abertram\AppData\Local\Microsoft\Windows\PowerShell\PowerShellGet\, or under one
 of the paths specified in PATH environment variable value. NuGet.exe can be downloaded from https://nuget.org/nuget.exe. Do you want PowerShellGet to install NuGet.exe now?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y
9:01:33 AM : PSScriptInfo is not specified in the script file 'C:\adbtest.ps1'. You can use the Update-ScriptFileInfo with -Force or New-ScriptFileInfo cmdlet to add the PSScriptInfo to the script file.

The PowerShell Gallery requires some metadata to be added to the script before it can be published. This metadata includes version, author, GUID, and so on. To add this required information, you can use the New-ScriptFileInfo command to create a new script from scratch or the Update-ScriptFileInfo command to update existing scripts. I always choose to create a new script from scratch just to forego the possibility of the Update-ScriptFileInfo command changing my script.

I’ll create a new PS1 script called AdamsTestScript.ps1 using the New-ScriptFileInfo command. I’ll then copy and paste the contents of my other script into this one.

PS> New-ScriptFileInfo -Path C:\AdamsTestScript.ps1 -Description 'This is a test description'
PS> Get-Content -Path 'C:\AdamsTestScript.ps1'

<#PSScriptInfo

.VERSION 1.0

.GUID 5611f619-12cf-4a85-863a-1b09f337e397

.AUTHOR abertram

.COMPANYNAME

.COPYRIGHT

.TAGS

.LICENSEURI

.PROJECTURI

.ICONURI

.EXTERNALMODULEDEPENDENCIES

.REQUIREDSCRIPTS

.EXTERNALSCRIPTDEPENDENCIES

.RELEASENOTES


#>

<#

.DESCRIPTION
 This is a test description

#>
param(
    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string]$Name
)

Write-Host $Name

I now have a script ready to be published. I’ll go ahead and try to publish it again using Publish-Script.

Publish-Script -Path C:\AdamsTestScript.ps1 -NuGetApiKey <ApiKey>

Now you should see that no error is returned. We can then check to see if the script actually got published by using the Find-Script command and giving it the name of the script without the PS1 extension.

PS> Find-Script -Name AdamsTestScript

Version    Name                                Repository           Description
-------    ----                                ----------           -----------
1.0        AdamsTestScript                     PSGallery            This is a test description

At this point, it’s avaiable to everyone! You can also see it via the Gallery portal as well.

managing PowerShell Gallery ItemsManaging PowerShell Gallery Items

Topics: IT insights, PowerShell, Scripting

Leave a Reply

Your email address will not be published. Required fields are marked *

THIS POST WAS WRITTEN BY Adam Bertram

Adam Bertram is a 20-year veteran of IT. He’s currently an automation engineer, blogger, independent consultant, freelance writer, author, and trainer. Adam focuses on DevOps, system management, and automation technologies as well as various cloud platforms. He is a Microsoft Cloud and Datacenter Management MVP and efficiency nerd that enjoys teaching others a better way to leverage automation.

Free Trials

Getting started has never been easier. Download a trial today.

Download Free Trials

Contact Us

Let us know how we can help you. Focus on what matters. 

Send us a note

Subscribe to our Blog

Let’s stay in touch! Register to receive our blog updates.