2.4. Make Yourself at HomeFor anyone who has spent significant time working in a shell, one thing quickly becomes apparent: there are some tasks that you repeat continuously until you finally break down and automate them with a script, often to be run at startup. Customizing your environment and creating your own shortcuts can significantly reduce the time spent on repetitive tasks and broaden the toolset at hand for solving new problems. In this section, we'll look at a special script called the profile that can be used to customize the shell. We'll bring together some of the techniques we've already covered, in addition to a couple of new ones, and we'll create a script file that will be run every time the shell is started.
2.4.1. How Do I Do That?It's time for the text editor again. We'll create a file called profile.msh in the MSH directory of your My Documents folder. We can edit this file as we would any other script, but its name and location have special meaning: when MSH starts up, it will look for this script and run it. For now, let's write a script that configures some aliases, sets up a couple of useful functions, and prints a welcome message, as shown in Example 2-3. Example 2-3. profile.msh in My Documents\MSH# my personal aliases set-alias ms get-member set-alias prop get-property set-alias displaytext write-object # my personal functions function prompt { "$((get-date).Month)/$((get-date).Day) " } function day { (get-date).Day } # increase the history buffer size $MaximumHistoryCount=1024 # welcome message "Welcome to MSH, " + $env:Username That's it. Next time you start the shell, the customizations enacted by profile.msh will be present, and you'll be greeted with a friendly welcome: Microsoft Command Shell Copyright (C) 2005 Microsoft Corporation. All rights reserved. Welcome to MSH, andy 8/15 $
2.4.2. What Just Happened?After the shell has loaded, but before it can accept user input or run any scripts, it goes about setting up its execution environment. From a logical point of view, this is done in three distinct stages. During the first stage, MSH runs the internal startup scripts and the system-level profile, which are stored in the Documents and Settings\All Users\MSH folder. This script takes the bare bones shell and makes it recognizable by defining a number of useful environment variables, familiar aliases, and commonly used functions. The second case is the main extensibility point (the one that we explored here). Putting customizations into a separate file makes them more manageable, easier to port to newer versions of MSH, and more convenient for copying to other machines. The third profile type enables a different profile script to be run when MSH is started on a specific machine or when it's based on some other criteria, such as access mode (imagine different behaviors based on whether you are accessing a machine physically or via a Terminal Services session). If you're set up with a roaming profile, it's sometimes convenient to create different profile scripts for the various machines you use, perhaps for the purpose of registering different file shares or modifying the available aliases. In this section, we also touched on a new conceptfunctions. Because the next chapter covers functions, we won't dwell on them here, but the prompt function is worthy of mentioning. By default, the MSH prompt includes the current location when waiting for input, such as cmd.exe. Instead of using a separate program or special escape sequences to customize the prompt, MSH can evaluate a script each time user input is needed. To this end, MSH evaluates the prompt function, which contains a script. Because this function is run every time the prompt is shown, it's a good idea to keep the script fast and simple; otherwise, you'll be waiting for it to evaluate after every command. 2.4.3. Where Can I Learn More?As you become more familiar with functions in the next chapter, you'll want to add the useful ones to your profile for easy access. There's one additional topic we need to cover before exploring more of the new features in MSH. |