PowerShell Profiles

24 Jul 2022 22678 views 0 minutes to read Contributors

 A PowerShell profile is a script that runs when I load an instance of PowerShell. I favor having nice little messages each time I load PowerShell, especially when they’re in the flavor of Futurama.

Creating Profiles in PowerShell

Let’s get a profile made and get the magic started. I can find out where my default profile is stored by typing $profile inside PowerShell. It is one of the built-in automatic variables (See help for more info on automatic variables).

 

 

By default, this file doesn’t exist. So, I’m going to need to create the file before I can add any code to it.

New-Item $profile -ItemType File -Force

 

 

Now that my profile is created, I can easily edit it with my preferred text editor. For simplicity, I’m going to use notepad. Running the following will open up that file via notepad.

Notepad $profile

 

Using PowerShell Profiles

Let’s put some code into it and see what happens. We can put any text in it, so I’ll add a nice quote to my $profile.

 

Closing and reopening PowerShell should kick off that extra code.

NOTE: Profiles are simply PowerShell scripts (.ps1 files) that run when PowerShell starts up, so the execution policy will be applied. If I have a restrictive execution policy, I will end up with an error when I start up my PowerShell console.

 

I’m going to take a deeper look at the $profile variable and see what else I can find. To do that, I’ll use Get-Member and investigate.

$profile | Get-Member

 

 

 

This is showing me that there are four profiles that can be used by PowerShell.

  • AllUsersAllHosts
  • AllUsersCurrentHost
  • CurrentUserAllHosts
  • CurrentUserCurrentHost

Breaking down the profile names a bit, the Current User and All Users portion seems straightforward enough for the profile type, but what does Current Host and All Hosts refer to?

Without diving too much into the weeds, each host application that runs PowerShell can have two sets of profiles that are specific to that host application, broken down by Current User and All Users.

That means that I should see two profiles for each program that implements a PowerShell runspace: PowerShell console, PowerShell ISE, Visual Studio Code, etc.

  • AllUsersCurrentHost (PowerShell, PowerShell ISE, Visual Studio Code)
    • C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
    • C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShellISE_profile.ps1
    • C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.VSCode_profile.ps1
  • CurrentUserCurrentHost (PowerShell, PowerShell ISE, Visual Studio Code)
    • C:\Users\hp\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
    • C:\Users\hp\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
    • C:\Users\hp\OneDrive\Documents\WindowsPowerShell\Microsoft.VSCode_profile.ps1

In addition to the two sets of profiles per host application, I have two profiles defined for all hosts.

  • AllUsersAllHosts
    • C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
  • CurrentUserAllHosts
    • C:\Users\hp\OneDrive\Documents\WindowsPowerShell\profile.ps1

This makes for 6 total profiles with just regular PowerShell and PowerShell ISE. In my case, I also have Visual Studio Code installed, so I have 8 total profiles to work with!

 

Report a Bug

In this article