Yesterday I showed how to add a menu choice to the PowerShell ISE to insert the current date and time. Today I have something even better. At least it's something I've needed for awhile. I write a lot of advanced functions in PowerShell. I'm often cutting and pasting from previous scripts and generally not being very efficient. Shame on me. What I needed when using the ISE was a way to load a function template. Here's what I came up with.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
First, I created a .ps1 file that defines a function called New-Function.
[cc lang="PowerShell"]
#requires -version 2.0
Function New-Function {
$name=Read-Host "What do you want to call the new function?"
$functionText=@"
#requires -version 2.0
# -----------------------------------------------------------------------------
# Script: $name.ps1
# Author: $env:userdomain\$env:username
# Date: $((get-date).ToShortDateString())
# Keywords:
# Comments:
#
# -----------------------------------------------------------------------------
Function $name {
<#
.Synopsis
This does that
.Description
A longer explanation
.Parameter
The parameter
.Example
PS C:\>
Example- accomplishes
.Notes
NAME: $($name)
VERSION: 1.0
AUTHOR: $env:userdomain\$env:username
LASTEDIT: $(Get-Date)
.Link
about_Windows_PowerShell_2.0
.Inputs
[String]
.Outputs
#>
[cmdletBinding()]
Param(
[Parameter(Position=0,Mandatory=`$False,
ValueFromPipeline=`$True)]
[string[]]`$var
)
Begin {
Write-Verbose -Message "Starting `$(`$myinvocation.mycommand)"
} #close Begin
Process {
Foreach (`$item in `$var) {
}#close Foreach $item
} #close process
End {
Write-Verbose -Message "Ending `$(`$myinvocation.mycommand)"
} #close End
} #end Function
"@
#load the function text into the current file in the ISE
$psise.CurrentFile.Editor.InsertText($FunctionText)
} #end function
[/cc]
The purpose of this function is to define a here string for the actual template, and then a command to insert it into the current file in the ISE.
[cc lang="PowerShell"]
$psise.CurrentFile.Editor.InsertText($FunctionText)
[/cc]
The New-Function function prompts you, using Read-Host, for the name of your new function and then inserts this value into the function text. I've also included some other variables that get expanded like the userdomain and username. For lines where I want the function to use an actual variable. I escaped the $ sign.
[cc lang="PowerShell"]
Write-Verbose -Message "Ending `$(`$myinvocation.mycommand)"
[/cc]
To test, you can manually load the New-Function function into the ISE. Then, with an open script, run New-Function from the prompt. You should get prompted for a name and end up with something like this. I entered Get-Answers when prompted.
[cc lang="PowerShell"]
#requires -version 2.0
# -----------------------------------------------------------------------------
# Script: Get-Answers.ps1
# Author: SERENITY\Jeff
# Date: 9/16/2010
# Keywords:
# Comments:
#
# -----------------------------------------------------------------------------
Function Get-Answers {
<#
.Synopsis
This does that
.Description
A longer explanation
.Parameter
The parameter
.Example
PS C:\>
Example- accomplishes
.Notes
NAME: Get-Answers
VERSION: 1.0
AUTHOR: SERENITY\Jeff
LASTEDIT: 09/16/2010 08:59:46
.Link
about_Windows_PowerShell_2.0
.Inputs
[String]
.Outputs
#>
[cmdletBinding()]
Param(
[Parameter(Position=0,Mandatory=$False,
ValueFromPipeline=$True)]
[string[]]$var
)
Begin {
Write-Verbose -Message "Starting $($myinvocation.mycommand)"
} #close Begin
Process {
Foreach ($item in $var) {
}#close Foreach
} #close process
End {
Write-Verbose -Message "Ending $($myinvocation.mycommand)"
} #close End
} #end Function
[/cc]
But since I don't want to always have to do that, I added a line in my ISE profile to first dot source the script.
[cc lang="PowerShell"]
. c:\scripts\new-function.ps1
[/cc]
Then I added a command to add an item to the Add-Ons menu.
[cc lang="PowerShell"]
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("New Function",{New-Function},"ALT+N") | Out-Null
[/cc]
Now, whenever I want to create a new function I can open a new window using Ctrl+N and then insert a new function with Alt+N.
Download a copy of New-function.ps1 and edit as necessary to meet your needs.
2 thoughts on “PowerShell ISE New Function”
Comments are closed.