PowerShell Tutorial 1: Configuring the PowerShell Console

PowerShell Tutorial 1: Configuring the PowerShell Console

This tutorial is an introduction to PowerShell, PowerShell concepts, and exercises in customizing the PowerShell Console. PowerShell Introduction: PowerShell is a new command shell from Microsoft and yes it is a command prompt and scripting environment, it can even be both at the same time. But what does PowerShell really mean for a systems administrator? What are the benefits in learning PowerShell? If you manage resources with VBScript you already know the answers. Active Directory domains are becoming larger and more complex. For those administrators who are still chained to GUI tools and the Microsoft Management Console, PowerShell will offer a more flexible environment. It has become a daunting task to manage resources in large domains, take a recent DST (Daylight Savings Time) change. Microsoft was slow to respond but did supply hot fixes for XP and Windows 2003. Windows 2000 was a different story, system admins had to rely on a manual edit using tzedit. PowerShell was designed for .NET objects; VBScript does not support a method for communicating or interacting with .NET. This Introduction was not based on what PowerShell is but what PowerShell, as a Shell and a Scripting Language, can do for you. The DST example is only the tip of the iceberg when it comes to the power of scripting languages. What PowerShell "is" you will discover as you continue learning. What's great about PowerShell, you will be able to use it right out of the gate to complete simple tasks. As your knowledge grows you will be able to take on more complex tasks.

PowerShell Concepts:

PowerShell is object-based not text-based This concept will take a little time grasp for dos/cmd.exe and batch script writers; those using VBScript and other programming languages already understand the concept. The basic difference, traditional command prompt output is text-based while output in PowerShell is not. It looks like text but it is actually an object. Why is this powerful? Because the outputs of a PowerShell command (the object) can be piped into another command without additional programming. With traditional scripting, if you wanted to use the output of one command in another, additional programming would be required to manipulate the data in a format the second command could understand. What is an object? This should sound familiar to the Windows Administrators out there... "Everything in an Active Directory Domain is an

object." Servers, Computers, Printers, Shares, Organizational Units, Security Groups, Group Policy Objects, Users, etc... With PowerShell we can interact with these objects to enumerate information (object-properties) and/or create, modify, or delete objects and/or object-properties (object-methods).

PowerShell Commands are customizable PowerShell commands are referred to as Cmdlets. With the installation of PowerShell there are over a hundred cmdlets for you to use. The PowerShell team, in their infinite wisdom, created aliases to allow us to use the traditional commands we have become accustomed to (dir, cd, del, copy, etc...). Even the UNIX guys get a break with provided aliases (ls, man, etc...). PowerShell allows you to create your own aliases as well as creating your own cmdlets. Yep, unlike dos/cmd and reskit exe's, PowerShell provides a method to create your own PowerShell cmdlets.

PowerShell is a Command line interpreter and a scripting environment In a nutshell you have the best of both worlds within PowerShell. DOS was a command line interpreter, enter command get output. Sure you could use batch files, but in reality a batch file just entered the commands for you. VBScript utilizes WSH (Windows Scripting Host); you can't enter VBScript code in a command prompt. With PowerShell not only can you enter commands, you can build script-blocks from the PowerShell command line. You will be doing this in later tutorials.

Windows PowerShell Console Let's take a look at the Windows PowerShell Console. Go to Start -> Programs -> Windows PowerShell 1.0 -> Windows PowerShell You can also launch PowerShell from a command prompt or in start -> run by simply typing powershell

You will notice the command prompt PS U:\> PS signifies that you are running PowerShell, U: is the drive letter of my home directory. Yours may be different. If you get this error, power shell module says execution of scripts can't be loaded its disabled:

Try the following steps and check if it helps: Right-click your PowerShell icon and select Run as administrator.

Check the current script execution policy by using the Get-ExecutionPolicy cmdlet. To do this, input "Get-ExecutionPolicy" (without quotes) and press Enter on your keyboard. PowerShell will return a value of Restricted.

To change the script execution policy, use the Set-ExecutionPolicy cmdlet. Input "Set-ExecutionPolicy unrestricted" (without quotes) and press Enter on your keyboard.

To ensure that the script execution policy has been changed, use the GetExecutionPolicy cmdlet again. PowerShell should return a value of Unrestricted.

You should now be able to run your PowerShell scripts.

Exercise 1: Customize a shortcut to point to a home directory As you have noticed my prompt points to U: Drive. I like to organize my scripts in one location where I can access them quickly. U: is a drive mapping to my home directory on a file server. I've chosen my file server as it is backed up nightly. In this exercise we configure the PowerShell Console to open in a directory called "MyScripts."

1. Create the following directory ? C:\MyScripts. 2. Create a PowerShell shortcut on your desktop. 3. Right-Click on the shortcut and choose Properties. 4. Under the Shortcut tab locate the Start in: text box. Type in the path

to the folder created in step 1. C:\MyScripts 5. Click OK. 6. Launch PowerShell from the desktop shortcut. PowerShell starts in the directory path we've chosen:

Feel free to choose the directory path of your choice. If your path has a space in it, don't forget to double-quote it. Example: "C:\Program Files\MyScripts." I will be working from the C:\MyScripts directory path throughout the tutorials.

Exercise 2: Customizing color and text-size Remember the DOS prompt with the blue background and yellow text? We are going to change the shortcut to those colors.

1. Right-click on the shortcut and choose Properties. 2. Choose the Colors tab. 3. You will see four radio buttons; Screen text, Screen background, Pop-

up text, and Pop-up background. The Screen buttons are selfexplanatory; the Pop-up buttons are for colors used in the History Buffer. 4. Set Screen text to yellow, Screen background to blue. 5. Launch PowerShell from the desktop shortcut. PowerShell runs in the colors we've chosen:

Next you are going to make the text larger so you do not have to squint. 1. Close the PowerShell Console by typing exit. 2. Right-Click the PowerShell shortcut and choose Properties. 3. From the Fonts tab, choose 12 x 16. Click OK. 4. Launch PowerShell from the shortcut.

You should see the following:

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download