Creating Powershell Profile

Creating Powershell Profile

I had a conversation with a friend today, talked about work, he told me that he has some scripts that he uses a lot in his day to day job, and thought about creating a script that loads all of them at once. The first thing I told him is “Why don’t you set up a profile?” he waited for a few seconds and told me that he doesn’t know about profiles. If you are reading this and feel the same, this post is for you 🙂 PowerShell profile is a script that runs every time we start the shell. They can have customization elements like windows size and colors, load modules functions, and scripts, or create an alias or any PS-providers. Creating a profile is much easier as you think. PowerShell has a global variable name $Profile that hold the profile file path. By default, the file does not exist even if the variable holds it. You can type $profile to see the location:


Almost every PowerShell editors have their own profiles VS Code: C:\Users\MyUser\Documents\WindowsPowerShell\Microsoft.VSCode_profile.ps1 Powershell ISE: C:\Users\MyUser\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

Creating Powershell Profile:

I say “profiles” as we have four profiles for each shell, but we won’t dive there. To create our profile, first we need to check if we have one already:

Test-Path $PROFILE

we can see we don’t have one, so we create one with new-item cmdlet:

New-Item -Type File -Path $PROFILE -Force
Directory: C:\Users\administrator\Documents\WindowsPowerShell
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 8/24/2018 11:36 AM 0 Microsoft.PowerShell_profile.ps1

Now you can verify the creation of the file with the same test-path we used earlier. if everything went ok, now you see “True”

Test-Path $PROFILE

After we created our profile file, all left for us is to edit it, with any app we want, you can quickly open the file with your favorite editor:

## Open with Powershell ISE
ISE $Profile
## Open with VS Code
code $Profile
## open with notepad
notepad $Profile

Here some ideas to add to your profile:

1.Change the prompt, and add [Admin] if its elevated session:

function prompt {
    $identity = [Security.Principal.WindowsIdentity]::GetCurrent()
    $principal = [Security.Principal.WindowsPrincipal] $identity
    $(if (test-path variable:/PSDebugContext) { '[DBG]: ' }
      elseif($principal.IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) { "[ADMIN]: " }
      else { '' }
    ) + 'Saggie Haim PS ' + $(Get-Location) +
      $(if ($nestedpromptlevel -ge 1) { '>>' }) + '> '

2. Change the color of the window and font:

## Background Color
$host.UI.RawUI.BackgroundColor = "black"
## Font color
$host.UI.RawUI.ForegroundColor = "red"

3. Add your name to the windows title:

## Keep the default title and add my name
$host.ui.RawUI.WindowTitle += " Saggie Haim"

4. Load modules and add custom functions:

## Test-Port function
function Test-Port {
    param (
    $result =(New-Object System.Net.Sockets.TcpClient($hostname, $port)).Connected
    return $result
## Import Modules
Import-Module ActiveDirectory

Now just save and open a new shell

There is no limit to what you can add to your profile. You can always add or remove new things from your profile. Use it as your toolbox, add some style and make it your own. If you want to read more about PowerShell profile, Microsoft did a great job with its Documentation. You can use get-help about_profiles or read the online version here.

Share Tweet Send