Today I am going to walk you through my PowerShell Profile and try to explain the basics of what a Profile is and how it can be used.
So what is a Profile, and where is it?
Well, basically a profile is a PowerShell script file (.ps1) that is stored in a preconfigured location and that gets executed every time PowerShell starts. This script enables you to customize your PowerShell session and predefine settings as you find them useful. There is a built-in variable, $Profile, which stores the location of the script that is executed every time PowerShell starts.
The profile file is located in the WindowsPowerShell folder in every users “My Documents” folder. It has to be named Microsoft.PowerShell_profile.ps1.
By default there is no profile and everything is left to their defaults, to create the profile script you can create the WindowsPowerShell folder and then use notepad to save a file containing some PowerShell commands in that folder. You can also use the New-Item cmdlet to create the folder and an empty file by typing:
New-Item –Path $Profile -Type File –Force
Now that we know what a profile is and where it is located we can go on and take a look at an example profile, it’s actually my profile so if you have any tips for me, feel free to leave a comment.
I am starting my Profile with setting some variables, some are predefined and some are custom, I use them for the following settings:
$MaximumHistoryCount is used to set the number of commands that are stored in History. Defaults to 64. Maximum 32767.
$Transcript is used to set the path for Start-Transcript
$env:PSModulePath is an environment variable (‘env:’) and it is used to customize the path where PowerShell will look for modules to import.
$cert is a variable I use to store my CodeSigningCert to sign scripts.
After that I use Get-Host to figure out which version of PowerShell is running in the current session, if version 3 is detected, I set some DefaultParameterValues using $PSDefaultParameterValues. After that I check if the current session was started with “Run as administrator” if it was, the Update-Help cmdlet is run.
Since I am writing a module I have a short function to just make my life a little easier, it signs the module file using Set-AuthenticodeSignature and then copies the file to some network share for my coworkers.
At the end I am creating a new PSDrive of the FileSystem PS provider, after creating the drive the current location is set to that drive. This is my working directory, it contains all the scripts and test files I need.
I pasted the file to http://poshcode.org, I really like their “embed code” feature.
Now again, this is my profile, I use it to simply show what can be done, since I am still a beginner I appreciate ideas and tips from anyone!