As you can tell, I am a big fan of Powershell. In this post, I will document my Powershell setup. This is mainly for my own notes, but if you can get something out of this, then well and good. I would expect this to change fairly rapidly, so I will be updating this post as my setup changes with the release of new software versions.
- I am currently using Windows Powershell 1.0 from here
- I use Powershell Community Extensions 1.1 from here
- Make sure you install their profile file. Their Profile file will set you up with all of their added functions, variables, etc. A lot of the following steps assume you have Powershell Community Extensions(PSCX) installed, along with their profile file.
- PSCX adds a lot of excelent cmdlets and functions. Read about it on their site.
- I then fix a Powershell Community Extensions, profile problem. I edit their VS2005 ps1 file by running this command from the Powershell command line
- notepad (Join-Path $Env:PscxHome 'Profile\Profile.ps1')
- Add the following code at line 33, just after the if statement that determines the base install path
- # Bail out if Visual Studio is not installed
if (!(Test-Path $regKeyPath)) {
return
}
- This stops an error on profile load if you do not have VS2005 installed.
- I like to keep all of my files that I need to back up under the "D:\Data" folder. This makes my backup scripts easier. I also do not like putting any of my personal files on the "C:\", as this allows me to rebuild the OS partition easily without fear of data loss. To that end, I edit the profile file, and add a new variable which points to my own personal profile folder located at "D:\Data\Scripts\Profile". I then put a command to run my personal profile file from that location. This means that I have only added two lines to the standard profile, which can be easily repeated after re-install.
- notepad $userprofile
- Add the following lines to the BOTTOM of the file.
- $DavidsProfileScriptsPath = "d:\Data\Scripts\Profile"
. (join-path $DavidsProfileScriptsPath 'Profile.ps1') - Obviously you will need to create your own profile file in the above location.
- In order to have the same profile for all users on my machines, I move my profile to the Powershell home folder. This is because I like to run some of my Powershell functions from sheculer, and from runas. If I leave the profile.ps1 file in the default location, it is only loaded for the userid used to install Powershell. By moving it to the powershell install folder, it is loaded for every console session for every user. Be careful if you do this that you do not have anything in your profile that you would not want other users of your PC to run.
- move $userprofile $PSHome
- Now restart powershell to load the new profile from the new location.
- I find that I cannot always remember all of the custom functions that I have created, especially after a break from using Powershell. To solve that problem, I have created a hints function. I do this by:
- Place the following lines in your personal profile file located at "d:\Data\Scripts\Profile\Profile.ps1"
- . (join-path $DavidsProfileScriptsPath "Hints.ps1")
Get-Hints
- Create the file "d:\Data\Scripts\Profile\Hints.ps1" with the following lines:
- Function Get-Hints {
" "
" "
" Tips"
" ========================================="
" <command> = <description>"
" <command> = <description>"
" "
" "
}
Set-Alias -name hint -value Show-Hints `
-description "Show the Hints for my own personal functions"
Set-Alias -name hints -value Show-Hints `
-description "Show the Hints for my own personal functions" - Yes, I know, I really should use the Write-Host function, but for now,
this keeps the hints file small, easy to read, and easy to edit. If I
ever considered adding colour to the hints, I would use the Write-Host
function.
Now I have all of my scripts in a location that will not be overwritten by future Powershell installs or Powershell Community Extensions Installs, and can be easily backed up with all of my other files. I get all of my scripts and the default PSCX profile when I run a powershell script under a different userID using RunAs, or the Windows Scheduler. I also have some hints showing my own functions when I start the console, and whenever I type "hint" on the command line.
You will have to add your own functions to your personal profile file. My current preference is to create new ".ps1" files in the same directory as my personal profile "D:\Data\Scripts\Profile", and dot source them in my "profile.ps1" in exactly the same method as the hints function above. I have a common functions ps1 file for generic functions, and specific task related functions get put into their own ".ps1" file based on their target functionality. For example, one ".ps1 file for Team Foundation System functions, one for maintaining my blog, etc.
Posted
May 27 2007, 08:03 PM
by
David