How to add version info to my powershell script?

Viewed 1021

I have a script, test.ps1, shown below. But I would like to be able to run: .\test.ps1 -version and have it return the current version of the script to me.

Is there a way to do this?

<#
.SYNOPSIS

Test
  
.DESCRIPTION

 Desc

.INPUTS

 None

.OUTPUTS

  None
  
.NOTES

  Author  : me
  Version : 1.0
  Purpose : PowerShell script test
  
#>

This did not work.

1 Answers

I would like to be able to run a .\test.ps1 -version and have it return the current version of the script to me. Is there a way to do this?

You can achieve this using a non-mandatory switch parameter to output the NOTES in your comment-based help. Here's an example:

<#
.SYNOPSIS

Test

.DESCRIPTION

Desc

.INPUTS

None

.OUTPUTS

None

.NOTES

    Author : me 
    Version : 1.0 
    Purpose : PowerShell script test

#>

param(
    [parameter(Mandatory=$false, HelpMessage="Display script version")]
    [switch]
    $version
)

begin {
    if ($version) {
        (Get-Help $MyInvocation.InvocationName -Full).PSExtended.AlertSet
        exit
    }
}
process { }
end { }

Now, when you run the following:

.\test1.ps1 -version

...you'll see your NOTES:

Author : me
Version : 1.0
Purpose : PowerShell script test

In addition, users can also see your version information (NOTES) using:

Get-Help .\test1.ps1 -Full

Hope this helps.

Related