Latest posts by Adam Bertram (see all)
- Create a certificate-signed RDP shortcut via Group Policy - Fri, Aug 9 2019
- Monitor web server uptime with a PowerShell script - Tue, Aug 6 2019
- How to build a PowerShell inventory script for Windows Servers - Fri, Aug 2 2019
The Gallery is typically known for just publishing modules, but you can publish scripts as well. To publish anything to the PowerShell Gallery, you'll first need to set up an account and get your API key. Once you've set up your account, you should be ready to go!
To proceed, you'll need to know your API key. The Publish-Script command requires it. You'll find that API key by visiting your profile in the PowerShell Gallery.
I'm now going to assume you've got your API key available and you've got at least PowerShell v5 with the PowerShellGet module installed. With those assumptions out of the way, let's now prepare a script for publishing.
I've got a sample script at C:\test.ps1 that looks like this:
The Publish-Script command requires two parameters: the script you'll be publishing and the API key that gives you access to your account. But as we try to publish the script as is, things don't work quite right.
PS> Publish-Script -Path C:\test.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:\test.ps1'. You can use the Update-ScriptFileInfo with -Force or New-ScriptFileInfo cmdlet to add the PSScriptInfo to the script file.
This doesn't work because the PowerShell Gallery requires some specific information at the top of every script published there. We can use the Update-ScriptFileInfo command to add this information to existing scripts or use the New-ScriptFileInfo command to create a blank script with the required information. To prevent the possibility of overwriting our previous script, I'll create a new script with New-ScriptFileInfo, add this information to it, and then copy and paste my existing script to this one.
PS> New-ScriptFileInfo -Path C:\AdamsTestScript.ps1 -Description 'This is a test description'
PS> Get-Content -Path 'C:\AdamsTestScript.ps1'
This is a test description
Now that I have created my new script with the correct comments added, I'll try to publish the script again using the same command.
Publish-Script -Path C:\AdamsTestScript.ps1 -NuGetApiKey <ApiKey>
You'll see this time you'll receive no error! You can also confirm the script was published by using the Find-Script command and specifying the name of the original script.
PS> Find-Script -Name AdamsTestScript
Version Name Repository Description
------- ---- ---------- -----------
1.0 AdamsTestScript PSGallery This is a test description
After the script is published, you can also see it under your Manage My Items section directly on the PowerShell Gallery site.