Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

PowerShell ISE New Function

Posted on September 16, 2010September 16, 2010

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.

Manage and Report Active Directory, Exchange and Microsoft 365 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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “PowerShell ISE New Function”

  1. Pingback: Tweets that mention PowerShell ISE New Function | The Lonely Administrator -- Topsy.com
  2. Pingback: Adding header to newly created scripts in PowerShell ISE

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d