2.4. Make Yourself at Home
For
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
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.
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
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
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. |