Quantcast
Channel: ISE Addons – Ravikanth Chaganti
Viewing all articles
Browse latest Browse all 34

Adding header to newly created scripts in PowerShell ISE

$
0
0
Here is a simple function that you can use to add a script header everytime you create a new script file in PowerShell ISE. Jeff Hicks posted a function to create an addon menu option to do this. What I am showing here does not add any menu option. This function uses $psISE.CurrentPowerShellTab events and adds the header only to untitled files.  $Header = @" #requires -version 2.0 # ----------------------------------------- # Script: # Author: $env:userdomain\$env:username # Date: $((get-date).ToShortDateString()) # # ----------------------------------------- "@ Function Add-Header { param ( [System.Management.Automation.PSEventArgs]$objEvent ) foreach ($newItem in $objEvent.SourceEventArgs.NewItems) { If ($newItem.IsUntitled) { #This is a dirty workaround for the issue reported on connect #https://connect.microsoft.com/PowerShell/feedback/details/600178/psise-currentfile-points-to-old-file-even-after-collectionchanged-event# foreach ($file in $psISE.CurrentPowerShellTab.Files) { if ($file.DisplayName -eq $newItem.DisplayName) { $file.Editor.InsertText("$Header`n") } } } } } Register-ObjectEvent -InputObject $psISE.CurrentPowerShellTab.Files -EventName CollectionChanged -SourceIdentifier NewFile -Action { Add-Header $Event } How this one works is quite simple. We do an event subscription for the CollectionChanged event of $psISE.CurrentPowerShellTab object. Once we receive this event, we use the Add-Header function to insert the script header. While writing this, I found an issue (hopefully.!) with …

Viewing all articles
Browse latest Browse all 34

Trending Articles