Friday, November 25, 2011

Create a PowerShell profile

When you add aliases, functions, and variables, you are actually adding them only to the current Windows PowerShell session. If you exit the session or close Windows PowerShell, the changes are lost. So before you begin create a PowerShell profile. After this you can add, functions and variables and the will be stored in your profile. For example:
How to determine if you have a profile? Simple, in the command shell, type in: $profile

Your profile will then be listed. It would normally resemble something like this: C:\Users\Your.Name\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

PowerShell has its own profile script, which is in the same location as the regular profile script by default. The only exception is that it is called: Microsoft.PowerShellISE_profile.ps1
If you so happen to have an existing profile, open it up in Notepad; otherwise create a blank text file using Notepad in the pointed located by the profile command. The profile, in reality, is simply a PowerShell script that gets executed whenever a shell is launched.
If you are going to create a profile file, you will need a Windows PowerShell folder in your Documents folder if it doesn’t already exist of course.

$Shell = $Host.UI.RawUI
$Shell.WindowTitle=”I Rule PowerShell”
$size = $Shell.WindowSize
$Shell.WindowSize = $size
$Shell.BufferSize = $size

Save this file and open a new PowerShell window. You will then be confronted with an error. No worries, this is simply Microsoft’s way of protecting you. PowerShell has the ability to allow its users to make malicious scripts and deliver them for others to run. Most of the scripts are of course disabled with the proper settings placed in PowerShell. PSH will not let you run any script, not even your profile, unless it has been signed using a trusted certificate issued by a Certificate Authority or a self-generated certificate using the Microsoft .NET Framework Software Development Kit (SDK).
For now, to get rid of the error to run your own script and see how it looks like, simply run the following command:

Set-ExecutionPolicy RemoteSigned

And by default the execution policy is Restricted. In other words, no scripts can run and only interactive commands are allowed. Again, it is for your own protection as well as others.
Once this has taken place, close the current PowerShell window and open a new one, you will see the title, color and window size change at once.

No comments: