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)
set-executionpolicy unrestricted |
Next, let’s make sure there is not already an existing profile script:
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:
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
notepad $profile |
In this example we will update the profile script to:
- Set the working directory to c:\powershell
- Create a function called split-path (more on that here)
- 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-pathnow 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>
Recent Comments