Creating a PowerShell profile
If you want certain PowerCLI commands to be executed every time you start a PowerCLI session, you can put these commands in a PowerShell profile. The commands in a PowerShell profile will be executed every time you start a new PowerCLI session. There are six PowerShell profiles, two specific for the PowerShell console, two specific for the PowerShell Integrated Scripting Environment (ISE), and two used by both the PowerShell console and the PowerShell ISE. The PowerShell console and the PowerShell ISE have their own profiles for:
- All users, current host
- Current user, current host
The two profiles used by both the PowerShell console and the PowerShell ISE are:
- All users, all hosts
- Current user, all hosts
You can retrieve the locations for the different profiles of the PowerShell console by executing the following command in the PowerShell console. In this command, the $PROFILE
variable is a standard PowerShell variable that returns an object containing the locations of the PowerShell profiles. This object is piped to the Format-List -Force
command to display all of the properties of the $PROFILE
object in a list:
PowerCLI C:\> $PROFILE | Format-List -Force
AllUsersAllHosts : C:\Windows\System32\WindowsPowerShell\v1.0\p
rofile.ps1
AllUsersCurrentHost : C:\Windows\System32\WindowsPowerShell\v1.0\M
icrosoft.PowerShell_profile.ps1
CurrentUserAllHosts : C:\Users\robert\Documents\WindowsPowerShell\
profile.ps1
CurrentUserCurrentHost : C:\Users\robert\Documents\WindowsPowerShell\
Microsoft.PowerShell_profile.ps1
Length : 76
As you can see in the output of the preceding command, the $PROFILE
object has four properties AllUsersAllHosts
, AllUsersCurrentHost
, CurrentUserAllHosts
, and CurrentUserCurrentHost
that contain the locations of the different profiles.
To list the locations of the PowerShell profiles of the PowerShell ISE, you have to execute the preceding command in the PowerShell ISE. This gives the following output:
PS C:\> $PROFILE | Format-List -Force
AllUsersAllHosts : C:\Windows\System32\WindowsPowerShell\v1.0\p
rofile.ps1
AllUsersCurrentHost : C:\Windows\System32\WindowsPowerShell\v1.0\M
icrosoft.PowerShellISE_profile.ps1
CurrentUserAllHosts : C:\Users\robert\Documents\WindowsPowerShell\
profile.ps1
CurrentUserCurrentHost : C:\Users\robert\Documents\WindowsPowerShell\
Microsoft.PowerShellISE_profile.ps1
Length : 79
Note
You can start the PowerShell ISE from a Command Prompt by running powershell_ise.exe
. You can start the PowerShell ISE from within a PowerShell console with the alias ise
.
The default value for the $PROFILE
variable is the value of the $PROFILE.CurrentUserCurrentHost
property. So you can use $PROFILE
instead of $PROFILE.CurrentUserCurrentHost
.
You can determine if a specific profile exists by using the Test-Path
cmdlet. The following command will test if the profile specified by $PROFILE
exists:
PowerCLI C:\> Test-Path -Path $PROFILE
False
If a profile does not exist, as in the preceding example, you can create the profile using the New-Item
cmdlet. If the directories in the path do not exist, by using the -Force
parameter the New-Item
cmdlet will create the directories. The following command will create the current user/current host profile and will also create the missing directories in the path:
PowerCLI C:\> New-Item -Path $PROFILE -ItemType file -Force
Directory: C:\Users\robert\Documents\WindowsPowerShell
Mode LastWriteTime Length Name
---- ------------------ ------ ----
-a-- 1/7/2017 2:01 PM 0 Microsoft.PowerShell_pro
file.ps1
After creating the PowerShell profile, you can edit the profile using the PowerShell ISE with the following command:
PowerCLI C:\> ise $PROFILE
If you put the commands from the preceding section, Modifying the PowerShell execution policy, the new colors of the messages will be used in all of your PowerCLI sessions.