Publish a PowerShell module to an Azure Artifacts feed
Register an Azure Artifacts feed as a private PowerShell repository and publish a module with PowerShellGet, PSResourceGet or nuget.
This article shows how to register an Azure Artifacts feed as a private PowerShell repository and publish a module or package to it, using PowerShellGet v2 (Register-PSRepository), PSResourceGet (Register-PSResourceRepository / Publish-PSResource) or nuget.exe. Authentication uses a personal access token (PAT) with packaging permissions. Never put the PAT in a script: store it in a secret vault.
Reference: Use a private PowerShell library with Azure Artifacts.
Placeholders
| Placeholder | Meaning |
|---|---|
<ORGANIZATION_NAME> |
Azure DevOps organisation |
<PROJECT_NAME> |
Project that owns the feed (omit if the feed is organisation-scoped) |
<FEED_NAME> |
Azure Artifacts feed |
<PAT> |
Personal access token with Packaging (read and write) scope |
Store the PAT in a secret vault
Register-SecretVault -Name "MySecretVault" -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault
# $credentials is a PSCredential: any user name, the PAT as password
$credentials = Get-Credential
Set-Secret -Name "MyCredential" -Secret $credentials -Vault "MySecretVault"
$CredentialInfo = [Microsoft.PowerShell.PSResourceGet.UtilClasses.PSCredentialInfo]::new('MySecretVault', 'MyCredential')
Option 1: PowerShellGet v2
$patToken = "<PAT>" | ConvertTo-SecureString -AsPlainText -Force
$myCredentialsObject = New-Object System.Management.Automation.PSCredential("user", $patToken)
Register-PSRepository -Name "PSGalleryUpstream" `
-SourceLocation "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v2" `
-PublishLocation "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v2" `
-InstallationPolicy Trusted -Credential $myCredentialsObject
Option 2: PSResourceGet
Register the feed with the credential stored in the vault, then list repositories and publish:
Register-PSResourceRepository -Name "PowershellPSResourceRepository" `
-Uri "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json" `
-Trusted `
-CredentialInfo $CredentialInfo
Get-PSResourceRepository
Publish-PSResource -Path C:\work\demo -Repository PowershellPSResourceRepository -ApiKey (Get-Secret MyCredential)
Publish-PSResource can also target a repository registered with Register-PSRepository (here PSGalleryUpstream) by changing -Repository.
Option 3: nuget.exe
Add the feed as a NuGet source (replace <PAT>; do not commit it):
nuget sources Add -Name "MyModuleFeed" `
-Source "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json" `
-username "user" -password '<PAT>'
Create a .nuspec for the module, pack it, then push the .nupkg:
& .\nuget spec Get-Hello
& .\nuget pack Get-Hello.nuspec
& .\nuget.exe push -Source "MyModuleFeed" -ApiKey '<PAT>' C:\work\demo\Get-Hello.1.0.1.nupkg
A successful push ends with:
Pushing Get-Hello.1.0.1.nupkg to 'https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json'...
PUT https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json
Using credentials from config. UserName: user
Accepted https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json 1325ms
Your package was pushed.
Remarks
- Each push needs a new package version: bump the version in the
.nuspec(or module manifest) before packing again. - Keep the PAT out of scripts and source control; use the secret vault or a pipeline secret variable.