PowerShell Profile Scripts

Directions-Training-Remoting-PowerShellPowerShell profile scripts are standard PowerShell scripts that are loaded and run automatically by the host applications, like PowerShell ISE, whenever the application is launched. The main benefit of profile scripts is that it allows the user to preconfigure their PowerShell environment to suit whatever task they will be performing in PowerShell.

PowerShell profile scripts can be used to automatically load modules, define variables and aliases or perform any other task that is needed to setup a ready-made PowerShell environment, thus eliminating the need to perform these tasks each time a PowerShell host application is launched.

In order for profile scripts to be automatically loaded and run, the script files must be saved to one of four locations. Each location has its own scope, as explained in the following table:Directions-Training-PowerShell-Profile-Scripts

The $home variable refers to the user account home folder, for example, C:\Users\Administrator, while the $pshome variable refers the PowerShell installation folder, which by default is at C:\Windows\System32\WindowsPowerShell\v1.0.

Other uses for profile scripts include setting the execution policy to allow your script to run without requiring the script to be signed: Set-ExecutionPolicy Unrestricted. Remember to set the execution policy back to its original setting before closing the PowerShell session.

Here’s a simple example of a PowerShell profile script:

Set-ExecutionPolicy Unrestricted
Set-Location D:\PowerShell
Import-Module ActiveDirectory
If ($host.UI.RawUI.WindowTitle –match “Administrator”)
($host.UI.RawUI.BackgroundColor = “DarkRed”;

#Welcome message
“You are now entering PowerShell, “+ $env.UserName

In the above script…

  • The execution policy is set to unrestricted to allow unsigned scripts to run.
  • The second line sets the Powershell execution context to the path specified; in this case D:\Powershell.
  • The third line clears the host console screen.
  • The fourth line imports the Active Directory PowerShell module.
  • The fifth line changes the background text color to dark red and the foreground text color to white if the console was launched under the Administrator context.
  • The last two lines are simply a welcome message that indicates the logged in user.

There are many uses for PowerShell profile scripts and the easiest way to identify good candidate profile scripts items it to think about those commands that you routinely perform whenever you open a PowerShell session. For instance, if you commonly need to perform tasks with Exchange 365 using PowerShell, you could create add a function to a profile script that will automatically connect to your Exchange 365 environment. This function would capture your Exchange credentials, create a new PSSession to Exchange 365 and then import that session.

See the article, PowerShell Function to Connect to Office 365 Exchange Online for details on creating this function and adding it to a profile script.

Think of profile scripts as a way to customize your PowerShell experience and have those customizations applied to your PowerShell session automatically. By leveraging this and all of the features that PowerShell offers, you can get down to the business of managing your environment with PowerShell without having to manage the PowerShell environment.

Leave a Reply