Customizing your environment — Aliases, and Dot-Sourcing

Since it takes a little while to get approved by BetaPlace, I'm going to hold of
on the answer to prompt customization until next time. However, we do have some
more cool things that will help you customize your environment.

The MSH shell is designed to be both verbose, and pithy. (Jeffrey's Channel9 interview
quote springs to mind.) We enable this through the use of aliases, like many other
shells.

Sure, it's great that you have descriptive commands such as "get-location" and "new-hostbuffercellarray".
They're great for reading, great for showing up as hits in help searches, and great
when you need to understand a script you wrote 6 months ago. However, I didn't learn
the Dvorak keyboard layout to spend my day typing 20-character commands.

Writing an alias for a command takes the form:

MSH:33 C:\>set-alias cls clear-host

Now, typing 'cls' has the same effect as typing 'clear-host.' Of course, 'set-alias'
is aliased to 'alias' to make your life easier, and sentences like this more difficult.

If you want to persist these changes, as you did with your prompt, store them in
your profile.msh. You'll notice that we define a great number of convenience commands
this way. Prefer aliases to obtain terseness, rather than naming your scripts this
way. Just as the verbose Monad cmdlet names help you find them during searches,
so will the titles of your descriptively-named custom scripts.

Now that you're getting a bit of customization done to your profile, it's probably
time to describe a healthy habit. That is, separating your profile into the part
that the Monad team ships, and the part that you've customized. We'll accomplish
this by putting all of our custom profile modifications into a separate file, called
profile-custom.msh.

1) In your editor of choice, create a new file called profile-custom.msh. Save it
to the same directory that contains your default profile.msh.
2) Take all of the changes you made to the default profile, and put them instead
into the profile-custom.msh.
3) Add the following lines to the bottom of profile.msh:

# Load our profile customizations
. (combine-path $MyDocuments \msh\profile-custom.msh)

The last step is called "dot-sourcing." Dot-sourcing works on both files and functions.
When you dot-source either of these, Monad treats the commands they contain as though
you had typed them inline. So, dot-sourcing your custom profile makes Monad treat
your customizations as though they were actully in-line with the rest of the profile.

Here's another example of dot-sourcing, this time using functions.

## Scratch.msh
##

function ExampleFunction
{
   $myVariable = 10
   "Hello World"
}

ExampleFunction
$myVariable

. ExampleFunction
$myVariable

Produces the result:

MSH:3 C:\temp>.\scratch.msh
Hello World
Hello World
10

Notice that the value of $myVariable is still available after dot-sourcing the function.
The same does not hold true when you call the function directly.

So, here's a question for those of you who have played with the shell so far: What
idioms from other languages / shells do you find the hardest to un-train yourself
from? For example, "cd.." is a valid command in DOS, but not MSH. What have you
found the hardest to discover? What did you do to try and discover it?  I'm
looking for things like,

"I couldn't figure out how to use a hash table, so I typed get-command *hashtable*"

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

6 Responses to “Customizing your environment — Aliases, and Dot-Sourcing”

  1. Jeffrey Snover writes:

    Here is a snippet of something I put in my profile file. It allows me to have a common profile.msh across a number of machines while also allowing each machine to have a unique setup.

    I use this for lots of things like setting up machine-specific drives.

    The first line uses the -f operator which is a string format operator.
    The second line tests whether a path exists or not (notice that it uses ~ [your home directory]).
    The last line then dot-sources the file into the current environment.

    # This allows you to have your own host-specific profile file
    $myHostSpecificStartup="~\msh\{0}.msh" -f $(hostname)
    if (test-path $myHostSpecificStartup)
    {
    . $myHostSpecificStartup
    }

  2. [email protected] (Ayende Rahien) writes:

    It’s me again, and I still didn’t get a way to enter the system or to get a download link for the beta.
    What is wrong?
    I just signed in again according to the instructions in the last post, but no link, just the "you must register".

  3. Lee Holmes writes:

    Hi Ayende; Sorry you’re having trouble enrolling in the beta. I’ll follow up with our beta coordinator to see if maybe the approval got lost somewhere — but youmight want to check your spam folder perhaps in the meantime.

  4. Ayende Rhaien writes:

    Already did, that was the first thing that I thought of.
    But I didn’t seem to get /any/ mail.

  5. Paul writes:

    I’ve found some of the examples I’ve found dont work, for example, on http://channel9.msdn.com/wiki/default.aspx/Channel9.MSHQuickStart
    it says that the following command should work:
    get-childitem *.txt | get-member -property

    But my computer doesn’t seem to know what the -property is:

    C:\get-childitem *.txt | get-member -property
    get-member : A parameter cannot be found that matches parameter ‘property’.
    At line:1 char:42
    + get-childitem *.txt | get-member -property <<<<

    Also the pick-object doesn’t seem to work either:
    C:\get-childitem *.txt | pick-object FullName
    : ‘pick-object’ is not recognized as a Cmdlet, function, operable program, or
    script file.
    At line:1 char:34
    + get-childitem *.txt | pick-object <<<< FullName

    Any hints?

  6. Lee Holmes writes:

    Hi Paul;

    The examples on the Wiki were written against our previous beta, so that’s probably what you’re experiencing.

    In general, to get help on anything (such as get-member,) try this:
    get-help get-member

    The syntax for what you’re looking for is this:
    get-member -membername Property
    which can be shortened to
    get-member -m property
    if you wish

    Also, we’ve renamed pick-object to select-object:
    get-childitem *.txt | select-object FullName
    also, those two cmdlets are in the default alias list, so you could write:
    dir *.txt | select FullName

Leave a Reply