PowerShell Profile : Customize the Shell experience

powershell

How to automate custom code execution every time we launch the Shell?

 PowerShell is an open source command line interface, widely promoted by Microsoft, enabling interaction with services and tasks automation.
In this article, we will dive into the PowerShell profile and use it to automate custom code execution every time we open the Shell.


What is the PowerShell Profile?

The PowerShell profile is a file .ps1 using a standard naming policy:


  • Microsoft.PowerShell_profile.ps1 (execute in PowerShell)
  • Microsoft.PowerShellISE_profile.ps1 (execute in PowerShell ISE)
  • Profile.ps1 (execute in both platforms)


Depending on the platform, you can launch the following CmdLet to retrieve the files path:

#The $PROFILE variable contains the Profiles files path
$Profile | format-list * -force
PowerShell Profiles path
Output for the CmdLet $Profile |format-list * -force

So we can create a PowerShell Profile file with a CmdLet by the use of this variable:

#Create the PowerShell Profile file for the current user and host
New-Item -Path $PROFILE.CurrentUserCurrentHost -Type file -Force

 When you launch PowerShell, it always search for a profile file to execute the code within.
You will find bellow the two dirrectories where PowerShell will search for those files:

PowerShell Profile path for current user :
%windir%\system32\WindowsPowerShell\v1.0\

PowerShell Profile path for all users :
%UserProfile%\My Documents\WindowsPowerShell\

This organization is pretty interesting because it allows us to differentiate the automatic execution of custom code depending on the platform and user.
The All users file can only be edited by an account with administrator privilege.


How does the PowerShell Profiles work?

Instinctively, we can ask ourselves what behavior to expect when creating multiple profile file?

For testing purpose, we can create every files integrating the code to show the path of the file:

write-host $MyInvocation.MyCommand.Definition -f green #With green for readability concerns

This manipulation allows us to highlight the execution sequence of the profile files:

When we start PowerShell

PowerShell profile sequence
Output in PowerShell

And PowerShell ISE

PowerShell ISE Profile sequence
Output in PowerShell ISE


A PowerShell profile, so what?

Now that we have seen how works the profile feature, here are some implementation ideas:

Change configurations:

#Change the execution Policy
Set-ExecutionPolicy RemoteSigned

#Change the location
Set-Location 'e:/Scripts'

#Create an alias for notepad
new-item alias:np -value C:/Windows/System32/notepad.exe

Load ressources :

#Load PSSnapin
Add-PSSnapin Microsoft.SharePoint.PowerShell

#Load an assembly
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#Load a module
import-Module SharePointPnPPowerShellOnline 

#Load ressources
Get-childitem "e:/scripts/*.ps1" |select-object -expandproperty FullName |% {. $_}
Get-childitem "e:/scripts/*.psm1" |select-object -expandproperty FullName |% {. $_}

Design / Fun:

#Rename the shell
$Shell = $Host.UI.RawUI
$Shell.WindowTitle="The matrix..."

#Show a custom message
Write-Host "Welcome $env:Username"

#Change the Shell UI
$Shell = $Host.UI.RawUI
$size = $Shell.WindowSize
$size.width=70
$size.height=25 #maximum 55
$Shell.WindowSize = $size
$size = $Shell.BufferSize
$size.width=70
$size.height=5000
$Shell.BufferSize = $size

#Change the Shell Color
$shell.BackgroundColor = “Gray”
$shell.ForegroundColor = “Black”

Clear-Host #To start with a clean Shell

Show a random get-help :

#Afficher un Get-Help random
get-help (Get-Command | get-random) -Full

I hope that you found what you were looking for in this article, do not hesitate to react in the section below.

See you soon in the blog !



References :
docs.microsoft.com
Similar articles :
howtogeek.com
computerperformance.co.uk
devblog.microsoft.com
To go further :
PowerShell Profile and task scheduler

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.