PowerShell Cookbook

Search

Categories

 

On this page

Archive

Blogroll

Disclaimer
I work for Microsoft.

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 216
This Year: 16
This Month: 0
This Week: 0
Comments: 523

Sign In

 Monday, June 13, 2005
Tuesday, June 14, 2005 5:43:13 AM (Pacific Daylight Time, UTC-07:00) ( )

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.]

Wednesday, June 15, 2005 4:15:58 AM (Pacific Daylight Time, UTC-07:00)
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
}
Jeffrey Snover
Thursday, June 16, 2005 8:22:14 PM (Pacific Daylight Time, UTC-07:00)
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".
Friday, June 17, 2005 2:56:34 AM (Pacific Daylight Time, UTC-07:00)
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.
Friday, June 17, 2005 10:27:57 AM (Pacific Daylight Time, UTC-07:00)
Already did, that was the first thing that I thought of.
But I didn't seem to get /any/ mail.
Wednesday, June 22, 2005 12:45:38 AM (Pacific Daylight Time, UTC-07:00)
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?
Paul
Wednesday, June 22, 2005 2:22:15 AM (Pacific Daylight Time, UTC-07:00)
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
Name
E-mail
Home page

Comment (Some html is allowed: b, blockquote@cite, em, i, strike, strong, sub, super, u)  

Enter the code shown (prevents robots):