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.
Signing Up for PowerShell Gallery
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.
Once you know your API key, you’re ready to go!
Publishing Your First Script
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.
If I try to publish this script as-is though, we soon run into a problem.
PS> Publish-Script -Path C:\adbtest.ps1 -NuGetApiKey
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
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
The Published Result
At this point, it’s available to everyone! You can also see it via the PowerShell Gallery portal as well.
Adam Bertram
Adam Bertram is a 25+ year IT veteran and an experienced online business professional. He’s a successful blogger, consultant, 6x Microsoft MVP, trainer, published author and freelance writer for dozens of publications. For how-to tech tutorials, catch up with Adam at adamtheautomator.com, connect on LinkedIn or follow him on X at @adbertram.