A profile script is user-specific startup script that will execute every time the owner opens a PowerShell session. This is very useful for those that want to customize their PowerShell environment.  You can use your profile script to issue any number of cmdlets, create aliases, set your working directory or whatever makes sense to you.

To get started, let’s make sure that you can run scripts. To do this, we must set your execution policy. The following command pulls out all the stops (and security features), allowing any script to run (view PowerShell help to see all options for this : help set-executionpolicy)

?View Code POWERSHELL
set-executionpolicy unrestricted

Next, let’s make sure there is not already an existing profile script:

?View Code POWERSHELL
get-content $profile

If any content was displayed then skip the next step and go directly to edit mode using “notepad $profile”.

Create a new Profile script (if needed) as follows:

?View Code POWERSHELL
New-Item -path $profile -type file -force

So far, so good – now let’s add some content to the profile script using the notepad editor

?View Code POWERSHELL
notepad $profile

In this example we will update the profile script to:

  1. Set the working directory to c:\powershell
  2. Create a function called split-path (more on that here)
  3. Create an alias called “path” that calls the split-path function

$Profile Contents:


set-location c:\powershell
function split-path {get-content env:path|% {$_.split(";")}}
new-item -path alias:path -value split-path

now save the file, exit notepad, and that’s it – voilà!
To test, open a new PowerShell window and enter the new “path” command. You should see something like this:


Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
CommandType     Name                                                Definition
-----------     ----                                                ----------
Alias           path                                                split-path
PS C:\powershell> path
C:\WINDOWS\system32
C:\WINDOWS
C:\WINDOWS\System32\Wbem
c:\program files\sysinternals
c:\program files\crimson editor
c:\program files\kstools
c:\Program Files\Microsoft SQL Server\90\Tools\binn\
c:\program files\jkdefrag
C:\Program Files\TortoiseSVN\bin
C:\Program Files\QuickTime\QTSystem\
C:\WINDOWS\system32\WindowsPowerShell\v1.0
PS C:\powershell>

If preferred, the console output from the new-item cmdlet can be hidden with the following modification to your profile script
change:

new-item -path alias:path -value split-path

to:

new-item -path alias:path -value split-path | out-null

Now, when you open your next PowerShell window, you will see the normal introductory banner:


Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\powershell>

 Leave a Reply

(required)

(required)

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>

   
© 2011 Posh TipsSuffusion theme by Sayontan Sinha

Page optimized by WP Minify WordPress Plugin