Windows PowerShell is Microsoft's latest command line and scripting language. It's a major move forward from the days on MS-DOS Batch files and gives Windows a scripting environment more in keeping with the likes of the
Bash Shell on Linux. Needless to say I like PowerShell and there are enough people out there already singing it's prasies what I want to highlight is the ability to customize the PowerShell environment for yourself.
Those of you comming from Unix or Linux will possibly have at one point encountered the '
.bash_profile'. This was a hidden file in the root of your home directory and allowed you to customize various settings suchas adding environment variables. This was extremely useful and I can recall adding various
JAR files to a classpath environment variable in a previous life. So when I came over to PowerShell and started writting my own scripts I wanted to may life easy for myself. Typing C:\users\alan\code\scripts is just too much like hard word even with tab complition. Wouldn't it be nice to just type 'cd $MyScripts'? Well it is and with tab completion of environment variables all I need to type is 'cd $mys' the tab key takes care of the rest.
PowerSehll has a hierarchl structure for profiles which allows profiles to be specified once for all users or for an individual user. It's simply a matter of knowing where on the hard drive to store the '
ps1' script. Your options are as follows:
- %windir%\system32\WindowsPowerShell\v1.0\profile.ps1
- %windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1
- %UserProfile%\My Documents\WindowsPowerShell\profile.ps1
- %UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Those profiles stored under the %windir% will effect all users whereas profiles stored under the %UserProfile% will effect only that user.
Ok so now we can look at a bit of PowerShell script to create the profile and begin customizing it.
test-path $profile
new-item -path $profile -itemtype file -force
notepad $profile
Note if you wanted to create a profile for all users replace the '-path' argument on the new-item cmdlet call with one of the paths listed above.
Now that you have a profile you can start customizing it, one of the most common things I've stored in my profile in the past has been environment variables. To do this with PowerShell simply use the Set-Variable cmdlet as shown below where I create a new variable called '
MyScripts' which will point to '
C:\Users\Alan\Code\PowerShell\MyScripts'
Set-Variable -Name MyScripts -Value "C:\Users\Alan\Code\PowerShell\MyScripts"