Using PowerShell Profiles

Page content

Some times I need to configure specific settings that will apply every time I open PowerShell or load certain functions or variables. This can easily be achieved by using a PowerShell profile script.

A profile script could be described as a startup script and is a script that will be run in current scope every time I start PowerShell, perfect for loading custom functions, settings or variables. The location of a users profile script is stored in the variable $profile which contains a string.

This string refers to what is called the CurrentUserCurrentHost profile script and can have a value like this: C:UsersSimonDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1.

Yep you guessed right, CurrentHost implies that the script only applies to one host. A host is the program hosting PowerShell and there are two hosts available by default in windows: powershell.exe and PowerShell ISE and each one has its own profile script.

Microsoft.PowerShell_profile.ps1 is the CurrentUserCurrentHost profile for powershell.exe. If I check the value of $profile in PowerShell ISE instead I get this value: C:UsersSimonDocumentsWindowsPowerShellMicrosoft.PowerShellISE_profile.ps1.

Both these files are located in the users Documents folder and is specific for each user. You can safely load your functions and customizations in these profiles without affecting any other user.

But wait, there is more! $Profile is a common string, but if we pipe it to the cmdlet Get-Member we can see that it has four extra noteproperties named:

AllUsersAllHosts

AllUsersCurrentHost

CurrentUserAllHosts

CurrentUserCurrentHost

Each of these has a value referring to a profile script and all of them are loaded each time you start PowerShell. They are loaded in the same order as listed above, meaning that if I set the same variable in both AllUsersAllHosts and CurrentUserCurrentHost, the value set in CurrentUserCurrent host will overwrite the value set in AllUsersAllHosts.

To show the paths to all profile scripts, pipe $profile to the cmdlet Select-Object with the parameter –Property *. This will show all properties on an object. Here is the output I get n powershell.exe:

image

If I run the same thing in PowerShell ISE I get this instead:

image

We can quickly see that the AllHosts profiles are the same on both pictures while the CurrentHost ones differ, meaning that there are actually six different profile scripts!

But that is still not all! If you are running a 64-bit OS, the AllUsers profile scripts are not the same for 32, –and 64-bit versions of PowerShell. If you start the 32-bit version of PowerShell, Windows PowerShell (x86), the AllUsers profiles are stored in C:WindowsSysWOW64WindowsPowerShellv1.0. This can come in handy if you have specific settings you only want to load when using the 32, –or 64-bit host, just keep in mind that these settings will apply to all users on the computer.

All in all there are nine profiles available! Here is the full list:

AllUsersAllHosts (x86)            : C:WindowsSysWOW64WindowsPowerShellv1.0profile.ps1
AllUsersAllHosts (x64)            : C:WindowsSystem32WindowsPowerShellv1.0profile.ps1

AllUsersCurrentHost (ISE x86)     : C:WindowsSysWOW64WindowsPowerShellv1.0Microsoft.PowerShellISE_profile.ps1
AllUsersCurrentHost (ISE x64)     : C:WindowsSysWOW64WindowsPowerShellv1.0Microsoft.PowerShell_profile.ps1
AllUsersCurrentHost (Console x86) : C:WindowsSystem32WindowsPowerShellv1.0Microsoft.PowerShellISE_profile.ps1
AllUsersCurrentHost (Console x64) : C:WindowsSystem32WindowsPowerShellv1.0Microsoft.PowerShell_profile.ps1

CurrentUserAllHosts               : C:UsersSimonDocumentsWindowsPowerShellprofile.ps1

CurrentUserCurrentHost (ISE)      : C:UsersSimonDocumentsWindowsPowerShellMicrosoft.PowerShellISE_profile.ps1
CurrentUserCurrentHost (Console)  : C:UsersSimonDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1

Before we can start customizing out PowerShell default settings we will have to create the profile scripts. I use the following snippet to create all profile scripts and open them in PowerShell ISE. This works from both the ISE and from powershell.exe

powershell_ise.exe -File ((
    $profile.AllUsersAllHosts,
    $profile.AllUsersCurrentHost,
    $profile.CurrentUserAllHosts,
    $profile.CurrentUserCurrentHost | 
    ForEach-Object {
        $Path = $_
        Try {
            $ParentPath = Split-Path $Path
            if(-Not(Test-Path $ParentPath)) {
                New-Item -Path $ParentPath -ItemType Directory -ErrorAction Stop
            }
            if(-Not(Test-Path $Path)) {
                New-Item -Path $Path -ItemType File -ErrorAction Stop
            }
            $_
        } Catch {
            Write-Warning -Message "Failed to create file $Path"
        }
    }
)-join ',')

Allright! Now that all profiles are created, what do I put in them?!

There is an excellent post on the Hey, Scripting Guy! Blog listing suggestions on what to put in your profile over here: http://blogs.technet.com/b/heyscriptingguy/archive/2014/05/22/what-s-in-your-powershell-profile-powershell-mvps-favorites.aspx

Happy Profiling!