0

Creating PowerShell Profile For Integrated Scripting Environment – ISE

In this post we will look at some of the mechanics behind creating a profile file which will customise the start-up behaviour of the Integrated Scripting Environment (ISE).  This will also serve as a precursor to additional posts on tweaking ISE for Exchange purposes.  The catalyst for writing this was twofold.  Firstly, an administrator was having issues trying to get ISE to process a profile script, and secondly, they were not connecting to Exchange PowerShell in a supported manner.

We will start off by looking at the default configuration, and seeing what happens with ISE when we use the location a lot of folks place their profile content in.  This is the $PROFILE location in Windows PowerShell.

Starting Configuration

The below screenshots are taken from a Windows 2008 R2 SP1 server.  Initially there are no PowerShell profiles on the system.  We are piping the $PROFILE variable to Format-List with the –FORCE parameter to show all of the profile paths.  Running $PROFILE on its own does not show all this detail.

$Profile | Format-List –Force

Exploring Windows PowerShell Profile Script Locations

Let’s go ahead and create a profile script in the $PROFILE location.  This is what the administrator I was working with was doing.

Creating PowerShell Profile

In the $PROFILE location, let’s create a script file that contains the below code.  This will write a phrase to the screen at the start and end of the profile script.  Additionally, it creates a variable called $Zorg that we can retrieve for testing purposes.

Write-Host “Starting profile” –ForegroundColor Magenta

$Zorg = “I am Zorg!”

Write-Host “Completed Profile” –ForegroundColor Blue

The file is created and saved:

Windows PowerShell Profile Script Contents

We can confirm the file is present in the directory.

Windows PowerShell Profile Script Created On Disk

Sweet!  let’s test this badboy in Windows PowerShell.

Testing In Windows PowerShell

Note that the script execution policy must be set to allow scripts to run.  Installing Exchange will set this automatically.  Else Set-ExecutionPolicy can be executed from an elevated Windows PowerShell session.  When we then launch Windows PowerShell, the profile file is processed.

Windows PowerShell Processes Profile Script

Yay!! All is good and now we can just load it up in ISE? Right?

Well not so much…..

Firing Up ISE

Launching ISE on the server produces less than spectacular results.  In fact, there is nothing.

ISE Does Not Process Windows PowerShell Profile Script

Did we not just see that the profile script was successfully launched?  Where is the disconnect?

Let’s go back and double check our file information.

Checking PowerShell Paths

Take a look at $Profile inside ISE.  Does it not have a $Profile variable??

<Phinius and Ferb>

      Yes, Yes  it does!

</Phinius and Ferb>

But is exactly the same?

Not The Droids We Are Looking For - Different Profile Name For ISE

This is the ISE $Profile

C:\Users\administrator.contoso\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

Compare this to the PowerShell $Profile

C:\Users\administrator.contoso\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

The files are in the same directory.  But note that the there is a difference between the file names.

Let’s remove the windows PowerShell profile and put our settings into the ISE profile.

Using ISE Profile Script

Firing Up ISE – Take 2

This time we have what we are looking for!

In the ISE results pane, we can see the starting and completed text.  In addition, we can also see that there is a variable called $Zorg and the contents are retuned as expected.

ISE Applies ISE Profile Script Settings

Digging In

Looking at the article How to Use Profiles in Windows PowerShell ISE we can see that it references the profile types that are supported.

Profile Type

ISE Path

Current user, PowerShell ISE $profile.CurrentUserCurrentHost, or $profile
“All users, PowerShell ISE” $profile.AllUsersCurrentHost
“Current user, All hosts” $profile.CurrentUserAllHosts
“All users, All hosts” $profile.AllUsersAllHosts

The designers or PowerShell were smart!  They gave us the capability to have different profile files.  Some apply universally across the server to all of the PowerShell hosts that are executed on that particular server.  Some are user and host specific.  In other words, we need to ensure that we are using the correct PowerShell profile script for the correct purpose.

In this post’s initial example, we had a disconnect between the $PROFILE variable which is the “current user, current host”.  There is a “current user, current host” profile script for BOTH ISE and also for Windows PowerShell.

A similar concept existed for VBScript as it could be hosted in Windows Scripting Host (WSH) or Internet Explorer.  Though it kind of lacked profile files, and a lot of other things as well compared to PowerShell….

If we look at examples of where the Windows PowerShell and ISE profile scripts are located, we can see that some of the profiles align and some do not.  Files which align are the AllHosts ones as they apply to all of the environments that can host PowerShell on a specific server.

 

Windows PowerShell Profile Locations

$profile.CurrentUserCurrentHost   ($Profile )

C:\Users\<profile>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

$profile.AllUsersCurrentHost

C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1

$profile.CurrentUserAllHosts

C:\Users\<profile>\Documents\WindowsPowerShell\profile.ps1

$profile.AllUsersAllHosts

C:WindowsSystem32WindowsPowerShellv1.0profile.ps1

 

 

ISE PowerShell Profile Locations

$profile.CurrentUserCurrentHost   ($Profile )

C:\Users\<profile>\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

$profile.AllUsersCurrentHost

C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShellISE_profile.ps1

$profile.CurrentUserAllHosts

C:\Users\<profile>\Documents\WindowsPowerShellprofile.ps1

$profile.AllUsersAllHosts

C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1

 

 

Conclusion

The key takeaway is that you need to use the right profile for the right purpose.  Should you only want the profile script to apply to Windows PowerShell then use either one of the CurrentHost profiles.  However, if the profile script is to only apply when using ISE then leverage one of the PowerShellISE profiles.

In the below example the same profile settings should apply to all instances on the server.  Thus, the AllUsersAllHosts profile was used.

In the below screenshot you can see that the Exchange Management Shell, Windows PowerShell and ISE have all processed the AllUsersAllHosts profile script.

Exchange Management Shell, Windows PowerShell & ISE All Processed AllUsersAllHosts Profile Script

In the next post we will see how to connect to Exchange 2010 and 2013 Remote PowerShell using ISE.

Happy profile scripting!

 

Cheers,

Rhoderick

Rhoderick Milne [MSFT]

Leave a Reply

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