Chris - Day 24

0 0 1 483 2756 Concentrated Technology 22 6 3233 14.0 Normal 0 false false false EN-US JA X-NONE

Today's chapter is over customizing PowerShells shell, its profiles, the prompt, and operators for working with strings. This is basically a mashup of all the little tidbits. But don't think that this is not important. All of these tips are very useful.


The First thing that we cover in this chapter is PowerShell profiles. Profiles allow you to setup your shell and ISE, load your commonly used modules and snap-ins, set alias' (if you're in to that sort of thing), set your starting directory, and many more things. Here is a copy of my ISE profile



                  Import-module ISEProfile




                  Set-Location "E:\Script-Stuff\PowerShell\Scripts"


One thing that I do have in my profile as you can see is a little tidbit I found on the Hey Scripting Guy Blog during my research, and that is the PowerShell ISE Transcript. The transcript allows me to log every command I type and every output of those commands and then appends them to a file. I have the start-transcript setup in my shell profile as well and I write those logs out so that I have a record of everything that I have typed (both good and BAD). I recommend anyone check this out. You can check that out here on his blog and just search for ISE Transcript or go to "".


You can put most anything into your profile including your own functions and modules.


Another cool thing that you can do is customize the prompt (C:\>) in the shell. This can be useful if you are adding time and date, or just something cool if you want. Here is the book function for changing the prompt for date and time.


                  PS C:\Users\Chris> function prompt {

                  >> $time = (get-date).Toshorttimestring()

                  >> "$time [$env:computername]:> "

                  >> }



And now our prompt would look like this: 5:43 PM [computername]:> . Pretty neat right.


You can also tweak the colors of the shell itself in its properties (change it from that blue) as well as the text that is presented inside it. Don lists these as the text settings you can change:

                  ERRORFOREGROUND and Background

                  WARNINGFOREGROUND and Background

                  DEBUGFOREGROUND and Background

                  VERBOSEFORGROUND and Background

                  PROGRESSFOREGROUND and Background


And you can change it to one of the following colors: Red, Yellow, Black, White, Green, Cyan, Magenta, Blue, or any of their DARK variants (DarkRed, DarkBlue, etc...).


Up next are Operators. There are lots of operators in PowerShell such as Comparison operators, Assignment operators, Arithmetic operators, and Pattern Matching and Text Manipulation Operators. In this chapter we only cover five of these.


-As and -Is. -As creates a new object from a current object or any object that you input. It does this by converting by Type. Such as to convert something to a [string], [int], [datetime], and so on and so forth. For more info on types see the About_Types help.


                  103.5 -as [int]         The thing about the -as and [int] is that it rounds to the nearest whole number.


-Is compares the input to a type and returns a true or false value.


                  104 -is [int]


All of the operators are fairly straight forward to use and as I said can be very useful. Look them up, try them out, and use them in your scripts.




Posted Apr 05 2012, 04:44 PM by Don Jones
Copyright 2012 All rights reserved.