PowerShell Not your Father’s Command Line Part 9 of 31: Another Side of PowerShell Profiles

imageIn some previous posts we talked about how to add functionality through importing modules, or adding aliases and functions  This allowed you to temporarily configure your PowerShell session to work the way you needed for that session. However, once you closed the PowerShell session all of your customization/configurations went away.  Profiles in Windows PowerShell provide the key ability to customize the PowerShell sessions.  This allows you to make your alias, functions and imported modules in a sense permanent, every time you load PowerShell.  This will prevent you from having to constantly recreate or reimport the components you need every time you use PowerShell.

A PowerShell profile is simply a script that runs every time PowerShell starts with the specific commands or elements you want your environment to always have.   With PowerShell you have essentially 4 profiles, that either effect all users and all shells or a the current user and the current shell…etc. (to learn more about the locations and impact take a look at help and run this command: Get-Help about_profile) .  For this post I am going to focus just on the default profile location which is for the current user and current shell.  The process to create the profile is the same, just the location for the actual profile script will determine its level of functionality.  To create a profile you will use the $Profile environment variable and your favorite text editor of all time, notepad.  To see the current location of the $profile variable is set at in your PowerShell session run:

$Profile

To create the profile at the current profile path run this command:

New-Item -path $profile -ItemType file –force

In your results you will notice the .ps1 extension name for the file designating this as a PowerShell script.  BTW by default this will cause a security issue you will see later in this post the first time you try to use your profile.  After the file has been created you can then edit the file in notepad with this command:

notepad $profile

You will then have a blank PowerShell script document to begin placing your various commands, functions…etc. into.  This will allow those command to load every time you load PowerShell.  You would type the same commands you would normally type in a PowerShell session for your profile.  For example if you put the following 3 lines (the examples from the previous posts) in your blank script in notepad, you would load the AppLocker module, have an alias called now and a function called Start-Shadow:

Import-Module AppLocker
Set-Alias now Get-Date
Function Start-Shadow {Start-Service VSS -verbose}

If you want to see an example of a profile script Sarah has used take a look at this post from one of the PowerShell sessions she recently delivered: Extending PowerShell – Notes from my 4/26 Presentation.  When you are done modifying your profile, save the file and restart your PowerShell session and all of the command elements you placed in your profile.  Most likely by default you would see a security message similar to the one below:

image

This indicates the current execution policy is set to prevent scripts from running in your PowerShell sessions.  In tomorrows post I will explain the meaning behind the message.  I will also explain what the following command does and how it allows you to get around this security message and potential impact on your system.  To run this command you will need to be in an administrative PowerShell session (Run As Administrator):

Set-ExecutionPolicy RemoteSigned

After you set the execution policy, restart PowerShell and then your profile will be at your fingertips.   The last tip for profiles is if you do not temporarily want to have your profile loaded for a PowerShell session you can start PowerShell with the –noprofile switch (your command would look like this powershell –noprofile).  You could simply modify the shortcut for PowerShell or run the command from your current PowerShell session.  If you no longer want to use the profile you can simply delete the file or remove all the commands to in make an empty script.

Again thanks for reading and if you missed any of the previous posts you can find a master list of series postings located here: PowerShell Not Your Father's Command Line: 31 Days of PowerShell or on Sarah’s blog here: PowerShell Not Your Father's Command Line: 31 Days of PowerShell. Lastly Sarah and I want to hear from you email either of us with your comments or suggestions for future postings let us know, we look forward to hearing from you. Have a great day!