How to make use of your PowerShell profile.

PowerShell is the tool for Windows Administrators. I can’t even begin to explain how truly useful it is in this article. Once of the most awesome features of PowerShell is it’s native extensibility. It is rather trivial to do things in PowerShell that would take monumental amounts of effort using any other Windows automation technology (I’m looking at you VBScript), if it is even possible to do them with said technology (again, I’m looking at you VBScript).

Before you can make use of a profile in PowerShell, you must enable the execution of scripts (this is disabled in PowerShell by default), by setting the Execution Policy to at least “AllSigned” (be aware that if you do this, you’ll have to be able to digitally sign your scripts before any will execute, including your profile script). You can find information on setting the PowerShell Execution Policy here.Once you have the Execution Policy set (I generally use RemoteSigned, but in a production environment, you should really be using AllSigned for maximum security), you need to set up your profile script.

PowerShell can store all kinds of useful things in the $profile variable (like aliases, functions, variables, or even extensions like snap-ins or modules), which is actually a PowerShell script that is executed when a console session is started (hence why you need to set the Execution Policy before you can make use of it). But the PowerShell Profile is not created automatically. To see if you have a PowerShell profile, open a PowerShell session, and type this command:

    test-path $profile

If it returned $true, you already have a PowerShell profile set up, if it returned $false, you do not (If you do skip to the “Editing your $profile” section below).

Creating a PowerShell Profile

If you do not already have a PowerShell Profile the first step is to create one. This can be easily done right from the PowerShell console. To create a profile script, type the following at the PowerShell console prompt:

    new-item -path $profile -itemtype file -force

This will generate the required file, and let PowerShell know that you are ready to use it.

Editing your $profile

Just having a PowerShell profile does nothing to change the way PowerShell behaves. To really make use of a PowerShell profile, you’ll need to add something to alter PowerShell’s default behavior. To edit the PowerShell profile type the following command at the PowerShell console:

    notepad $profile

This will open your $profile script for editing in NotePad. Ok, so now what do we put in there?

Really this is going to be personal preference. There are literally thousands of things you can put in a PowerShell profile script. For purposes of demonstration, we’ll set an alias. In the NotePad window that opened when we ran that last command, add this line:

    Set-Alias open 'explorer'

This creates an alias for Windows Explorer. Now save the file, and run this command at the PowerShell console:

    . $profile

This will reload your profile script (make sure there is a space between the “.” and “$profile”), and we can now make use of the alias. At the PowerShell console type the following:

    open c:\

If your $profile is working correctly, this should have opened the root of your C:\ drive in Windows Explorer. Now you can do the same thing without the $profile script directly in the console, but using the PowerShell Profile allows things you set there to persist between console sessions (you can test this by closing the PowerShell console and reopening it, the alias we just set should still work).

%d bloggers like this: