Show me your profile !

Presentation of the different PowerShell profiles

Show me your profile !

Presentation of the different PowerShell profiles

Show me your profile !


This article was written for a presentation of the French Powershell UserGroup

This presentation can be seen on Youtube on the FRPSUG channel

What is a profile?

Obviously this is the first question we can ask ourselves ;-)

A PowerShell profile is simply a script:

  • with a special name
  • which is in a special directory
  • which runs when PowerShell starts

The (or rather) profile is used to customize your environment:

  • add aliases
  • add functions
  • connect drives

The different types of profile

In the previous point, I am referring to several profiles, indeed there is a profile for each host:

  • Windows Powershell
  • Powershell (core)

but there is also a profile that applies to all hosts

As with host profiles, there is a profile for all users of a machine or for the current user.

So in summary a profile applies:

  • to the currently used host (CurrentHost) or to all hosts (AllHosts)
  • to the user currently logged in (CurrentHost) or to all users (AllUsers)

The possible combinations are therefore:

  • AllUsers, AllHosts
  • AllUsers, CurrentHost
  • CurrentUser, AllHosts
  • CurrentUser, CurrentHost

The above order is not trivial, it is the order in which the profiles are loaded when starting a console

To address these different profiles we use the variable $PROFILE as follows:

  • AllUsers, AllHosts => $PROFILE.AllUsersAllHosts
  • AllUsers, CurrentHost => $PROFILE.AllUsersCurrentHost
  • CurrentUser, AllHosts => $PROFILE.CurrentUserAllHosts
  • CurrentUser, CurrentHost => $PROFILE.CurrentUserCurrentHost

By default when we speak of the variable $PROFILE we speak of the variable $PROFILE.CurrentUserCurrentHost

Each combination is stored in a particular directory (case of Windows PowerShell and PowerShell Core):

  • $PROFILE.AllUsersAllHosts => $PSHOME\Profile.ps1
  • $PROFILE.AllUsersCurrentHost => $PSHOME\Microsoft.PowerShell_profile.ps1
  • $PROFILE.CurrentUserAllHosts => $Home[My]Documents\PowerShell\Profile.ps1
  • $PROFILE.CurrentUserCurrentHost => $Home[My]Documents\PowerShell\Microsoft.PowerShell_profile.ps1

$PSHOME refers to the PowerShell installation directory: C:\Windows\System32\WindowsPowerShell\v1.0 for Windows PowerShell C:\Program Files\PowerShell\7-preview for PowerShell Core

In the case of other environments (ISE, VSCode) the paths are identical but the file names can be different

How to create a profile?

The first step is to check if a profile file is not already existing

Test-Path -Path $PROFILE

If the result is False, just create the file

New-Item -ItemType File -path $PROFILE -force

Then just edit it with any text editor (notepad, notepad++, VSCode)

notepad $PROFILE

But what do we put in a profile?

As we have seen, the PowerShell profile is a script like the others. So we can put everything that can be found in a “normal” script

For example in the profile $PROFILE.AllUsersAllHosts on my machine I have this function

#region <Admin or Not>
function IsAdmin
    $CurrentUser =
    $principal = New-Object System.Security.principal.windowsprincipal($CurrentUser)

# Personalized greeting
if (isAdmin)
    $ElevationMode = 'Privilegie'
    $host.UI.RawUI.BackGroundColor = 'DarkRed'
    $ElevationMode = 'Non Privilegie'
    #$host.UI.RawUI.BackGroundColor = 'DarkMagenta'

$CurrentUser =

Write-Host '+---------------------------------------------------+'
Write-Host ("+- Hello {0} " -f ($CurrentUser.Name).split('\')[1])
Write-Host '+---------------------------------------------------+'
#endregion <Admin or Not>

This function allows you to display in the window title when I launch the console as administrator

Administrator Mode

or as a normal user

Normal Mode

j'ai également une fonction d'installation de module

$private:AutoLoad = @{
    Modules = @(

Write-Host "=> loading modules from AllUsersAllHosts"
Foreach ($private:M in $AutoLoad.Modules)
    if (!(Get-Module -ListAvailable -Name $m))
        Write-Warning "$($m) not found => installation launch "
        Install-Module -Name $m -AllowClobber -Force -ErrorAction SilentlyContinue -Scope CurrentUser

It is a module that I use for the compilation of my builds when I code.

For more example on profiles you can do a google search.

See also