Creating a PowerShell Profile

You can create a PowerShell profile to set your environment (aliases, modules you want to import, functions,etc..)

First of all check if a profile already exists with the command

test-path $profile

This will return true or false. If no profile exists you can create one with the following command:

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

This will create a Microsoft.PowerShell_profile.ps1 file in “%UserProfile%\My Documents\WindowsPowerShell”. The commands you enter in this file will apply to the current user and thePowerShell Shell.

You can now edit this profile with notepad, just type:

notepad $profile

I just entered the command import-module ActiveDirectory, this will automatically load the AD module whenever I start PowerShell.

note:
Their are 4 different profiles in PowerShell:

%windir%\system32\WindowsPowerShell\v1.0\profile.ps1
This profile applies to all users and all shells.

%windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1
This profile applies to all users, but only to the Microsoft.PowerShell shell.

%UserProfile%\My Documents\WindowsPowerShell\profile.ps1
This profile applies only to the current user, but affects all shells.

%UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
This profile applies only to the current user and the Microsoft.PowerShell shell.

Edit:
I just received some nice scripts from @shayLevy (thx) via Twitter.
Check these out (V2 scripts):

  • $profile|gm -MemberType noteproperty | select -expand name | foreach {$profile.$_}
    A nicer one :-)
  • $profile|gm -MemberType noteproperty |select @{n=’Name’;e={$_.name}},@{n=’Path’;e={$profile.($_.name)}}
    And last but best
  • $profile | gm -m 8 |select @{n=’Name’;e={$_.name}},@{n=’Path’;e={$profile.($_.name)}},@{n=’Exist’;e={Test-Path $profile.($_.name)}}

Scripts from @ShayLevy

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>