Skip to content

Instantly share code, notes, and snippets.

@DBremen
Created August 24, 2015 14:55
Show Gist options
  • Save DBremen/ae270d78e2a469398ddf to your computer and use it in GitHub Desktop.
Save DBremen/ae270d78e2a469398ddf to your computer and use it in GitHub Desktop.
Create default code template for PowerShell ISE
function Edit-ISETemplate{
$ISETemplatePath = "$([Environment]::GetFolderPath('MyDocuments'))\WindowsPowerShell\ISETemplate.ps1"
if (!Test-Path){
New-Item $ISETemplatePath -ItemType File
}
psedit "$ISETemplatePath"
}
Register-ObjectEvent $psise.CurrentPowerShellTab.Files CollectionChanged -Action {
# files collection
$ISETemplate = "$([Environment]::GetFolderPath('MyDocuments'))\WindowsPowerShell\ISETemplate.ps1"
if (Test-Path $ISETemplate){
try{
$sender |
where {$_.IsUntitled -and $_.Editor.Text.Length -eq 0 } |
foreach {
$_.Editor.Text = Get-Content $ISETemplate -Raw
}
}
catch {
write-error $_
}
}
} -SourceIdentifier CodeTemplate
@MateuszNad
Copy link

Hi. You have little mistake in line 3. You forgotten about variable $ISETemplatePath. That will be better
if (-not (Test-Path -Path $ISETemplatePath)){

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment