Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Archives
May 15, 2026

Profile Scripting for VSCode

In this issue:

  • New File
  • New Region
  • Location Jumping
  • Register-Command
    • Open-FileLocation
    • Optimize Manifest
  • Summary

Last time we started exploring profile script options for VS Code. I spent some time introducing you to some programmatic elements that you might want to take advantage of in VS Code. Today, let's start using some of those ideas in functions that we want to use in VSCode.

There's no reason you can't load functions in your VSCode profile that you want to run from the integrated PowerShell terminal. First, here's a quick function to create a new script file in VSCode.

New File

Function New-VSCodeScriptFile {
    [cmdletbinding()]
    [alias("nf")]
    Param()
    $initContent = @"
#requires -version 7.6

Param()


"@
    $psEditor.Workspace.NewFile($initContent)

    # Move cursor position to the end of the file using $psEditor in VS Code
    $ctx = $psEditor.GetEditorContext()
    $lastLine = $ctx.CurrentFile.FileRange.End.Line
    #set cursor to the first column in the last line
    $ctx.SetSelection($lastLine, 1,$lastLine,1)
}

The function creates an unsaved file with minimal content and then moves the cursor to the end of the file.

New Region

This is so that I can use my next function to insert named regions.

Function New-Region {
    [cmdletbinding()]
    [alias('nr')]
    Param(
        [Parameter(
            Position = 0,
            Mandatory,
            ValueFromPipeline,
            HelpMessage = "Specify the region start description"
        )]
        [ValidateNotNullOrEmpty()]
        [string]$Heading,
        [switch]$Passthru
    )
    Begin {
        $ctx = $PSEditor.GetEditorContext()
    }

    Process {
        Write-Verbose "Adding region $Heading"
        $r = @"
#region $heading


#endregion

"@

    } #process
    End {
        if ($Passthru) {
            #show the region in the console
            $r
        }
        $ctx.CurrentFile.InsertText($r)
    }
}

This function is intended to quickly enter regions in the currently open file. With these commands I can quickly setup a new script file. I take advantage of aliases to save typing.

PS C:\> nf
PS C:\> nr Initialize
PS C:\> nr "Validate data"
PS C:\> nr "Process data"
PS C:\> nr "Cleanup resources"

Although, after creating the file, I need to manually set the focus back to the terminal using the Ctrl+J shortcut.

The result is this script file ready for me to edit.

#requires -version 7.6

Param()

#region Initialize


#endregion
#region Validate data


#endregion
#region Process data


#endregion
#region Cleanup resources


#endregion

Location Jumping

Another function I use often makes it easy to quickly change locations based on the location of the current file.

Function Set-LocationToCurrent {
    #set location to the directory of current file
    [CmdletBinding()]
    [alias('sd', 'jmp')]
    Param ()

    $Context = $PSEditor.GetEditorContext()
    $ThisPath = $Context.CurrentFile.Path
    $target = Split-Path -Path $ThisPath
    $psEditor.Window.SetStatusBarMessage("Jumping to $target",2000)
    Set-Location -Path $target

    Clear-Host
}

Again, typing sd in the integrated PowerShell terminal sets my location to the directory of the currently active file. This saves me a lot of typing. By the way, did you see how I'm using the status bar message?

These are examples of functions I have in my VSCode profile script that I run from PowerShell. But there is another option to consider.

Want to read the full issue?
GitHub
Bluesky
LinkedIn
Mastodon
jdhitsolutions.github.io
Powered by Buttondown, the easiest way to start and grow your newsletter.