Changing the Location of PowerShell Profile Scripts

A PowerShell profile is an init script that is executed when the shell starts. PowerShell searches for profile scripts in hard-coded locations. This article explains how to move profile scripts to any directory of your choice.

Where are Profile Scripts Located?

As documented here, PowerShell has per-machine in addition to per-user profile scripts. We’ll ignore the former and focus on the latter.

User Profile Scripts

Per-user PowerShell profile scripts are stored in the Windows user profile. The path and name depend on the PowerShell version as well as the PowerShell host (e.g., Windows Terminal, VS Code). Here’s a quick overview:

PowerShell versionPowerShell hostPathFile name
Windows PowerShell (5.x)all hosts%USERPROFILE%\Documents\WindowsPowerShellprofile.ps1
PowerShell (7.x)all hosts%USERPROFILE%\Documents\PowerShellprofile.ps1
Windows PowerShell (5.x)console host, Terminal%USERPROFILE%\Documents\WindowsPowerShellMicrosoft.PowerShell_profile.ps1
PowerShell (7.x)console host, Terminal%USERPROFILE%\Documents\PowerShellMicrosoft.PowerShell_profile.ps1
Windows PowerShell (5.x)VS Code%USERPROFILE%\Documents\WindowsPowerShellMicrosoft.VSCode_profile.ps1
PowerShell (7.x)VS Code%USERPROFILE%\Documents\PowerShellMicrosoft.VSCode_profile.ps1

Changing the Location of PowerShell Profile Scripts

The symlink technique I used for Windows Store apps like Windows Terminal doesn’t work well with PowerShell because it breaks dot-sourcing (including additional scripts from a path relative to the symlink target). We can use dot-sourcing as a simple alternative, though.

Source

Using the table above, determine the path and file name of the profile script whose location you want to change. For the PowerShell 7 all hosts script, for example, that would be:

%USERPROFILE%\Documents\PowerShell\profile.ps1

Create that as a new (empty) file and open it in an editor.

Target

In the above profile script, we place a single instruction to include the actual profile script from the target directory, e.g.:

. 'D:\Data\PowerShell\profile_v7.ps1'

That’s it! We effectively redirected from %USERPROFILE%\Documents\PowerShell\profile.ps1 to D:\Data\PowerShell\profile_v7.ps1.

Comments

Related Posts

Obsidian: Getting Started With the Markdown Note-Taking App

Obsidian: Getting Started With the Markdown Note-Taking App
Ever since the original Microsoft OneNote was discontinued I’ve been on the lookout for my ideal note-taking app. Having settled on Markdown as the perfect file format I’ve tried numerous apps and occasionally blogged about the experience. In my quest to find the app that combines a great UX with elegant esthetics, speed and full keyboard control I’ve recently settled on a combination of Typora and Obsidian - none of which are perfect, but both are satisfying tools to use and they even complement each other. This article explains how to get started with Obsidian.
Applications

Latest Posts

Filebrowser to Be Archived. Migrate to a Modern Alternative: Sambee

Filebrowser to Be Archived. Migrate to a Modern Alternative: Sambee
Filebrowser is being archived. As the author writes, the codebase dates back to when he was but 15 years old. Having created such a popular file management tool is a more than impressive achievement for anyone, let alone a teenager. However, when it comes to security and reliability, Filebrowser seems to be built on a less than ideal foundation, so much so that the author states:
Home Automation, Networking & Self-Hosting