PowerShell Profiles: Location and Examples
In this tutorial, we’ll learn about PowerShell profiles. We’ll learn about the different types of profiles, where they are located, and how to use them to customize the PowerShell environment.
About Profile Scripts
A PowerShell profile is a script that runs automatically every time you start a PowerShell session. You can use it to import modules, set variables, create aliases, and customize the PowerShell prompt.
PowerShell Has Four Different Profiles
The location of your profile is stored in the $profile
variable.
$profile
In my case, it's Microsoft.PowerShell_profile.ps1
and it's located in my home directory.
However, this is not the only profile file. There are other profiles that apply to different levels. To see all of them, you can pipe the $PROFILE
variable to the Select-Object
command.
$profile | Select-Object *
The command displays a list of profile files and the levels at which they apply.
For example, the first one, profile.ps1
, is located in the installation directory and affects all users and all hosts.
If you’re not familiar with the term 'host,' a host in PowerShell refers to any application that provides an interface to run PowerShell scripts and commands.
For example, the PowerShell console I’m using right now is a host. Other examples include the PowerShell extension in Visual Studio Code and PowerShell ISE, which are commonly used to write and debug scripts.
So, when it says 'all users and all hosts,' it means this profile applies to all users on this computer and to all interfaces or hosts that use this version of PowerShell.
And one more thing: different versions of PowerShell have different profiles. So far, I’ve used PowerShell 7, but if I open an older version and check the $PROFILE
variable, it will have an entirely different set of profiles.
How to Edit and Use PowerShell Profiles
As we’ve learned, there are four different profile files, but the one we typically use is the one returned when you run the $PROFILE
command.
By default, both the file and the path may not exist. In that case, you’ll need to create them manually.
When creating the file make sure it ends with the .ps1
extension. If I use File Explorer, it might end up with the .txt
extension. So avoid creating it through File Explorer; instead, run one of the following commands:
notepad $profile
New-Item -ItemType file -Path $profile
Change PowerShell Execution Policy to Allow Profile Scripts to Run
And one more thing, you need to change PowerShell Execution Policy to allow profiles scripts to run. You can do that by running the following command:
Set-ExecutionPolicy RemoteSigned CurrentUser
To learn more about execution policies, click the link below.
How to Set PowerShell Execution Policy: Full GuideSetting Variables and Aliases in the PowerShell Profile File
You can use PowerShell profiles to customize your environment by creating variables, aliases, and custom functions.
Define your variable in the profile file. For example:
$var1 = "Hello, PowerShell!"
Use Set-Alias
to create aliases. For example, the following command sets the alias ll to run the Get-ChildItem
command, which lists files and directories in PowerShell.
Set-Alias ll Get-ChildItem
Reloading the Profile
Once you make a change to a profile, it won’t take effect until you restart PowerShell or reload the profile.
Restarting PowerShell means closing the console and reopening it. Reloading the profile means executing the profile file.
The following command will reload the profile file specified in the $PROFILE
variable.
. $profile
What Next?
Now you know how to locate your PowerShell profile, create or edit it, and use it with practical examples.
If you're interested in learning more about PowerShell, don’t forget to check out our PowerShell tutorial series.
You can also find video tutorials on our YouTube channel, @RealClearComputing.