PowerShell for the IT Administrator Part 1 Lab Manual v1.1

  • Upload
    birroz

  • View
    236

  • Download
    0

Embed Size (px)

Citation preview

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    1/360

     

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    PowerShell for the IT

    Administrator, Part 1 

    Student Lab Manual (v1.1) 

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    2/360

     

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Conditions and Terms of Use

    Microsoft Confidential - For Internal Use Only

    This training package is proprietary and confidential, and is intended only for uses described in the training materials.

    Content and software is provided to you under a Non-Disclosure Agreement and cannot be distributed. Copying or

    disclosing all or any portion of the content and/or software included in such packages is strictly prohibited.

    The contents of this package are for informational and training purposes only and are provided "as is" without

    warranty of any kind, whether express or implied, including but not limited to the implied warranties of merchantability,fitness for a particular purpose, and non- infringement.

    Training package content, including URLs and other Internet Web site references, is subject to change without notice.

    Because Microsoft must respond to changing market conditions, the content should not be interpreted to be a

    commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after

    the date of publication. Unless otherwise noted, the companies, organizations, products, domain names, e-mail

    addresses, logos, people, places, and events depicted herein are fictitious, and no association with any real

    company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should

    be inferred.

    © 2012 Microsoft Corporation. All rights reserved.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    3/360

     

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate

    Microsoft Confidential

    Copyright and Trademarks

    © 2012 Microsoft Corporation. All rights reserved.

    Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering

    subject matter in this document. Except as expressly provided in written license agreement from Microsoft, the

    furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other

    intellectual property.

    Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under

    copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted

    in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose,

    without the express written permission of Microsoft Corporation.

    For more information, see Use of Microsoft Copyrighted Content at

    http://www.microsoft.com/about/legal/permissions/  

    Microsoft®, Internet Explorer®, and Windows® are either registered trademarks or trademarks of Microsoft

    Corporation in the United States and/or other countries. Other Microsoft products mentioned herein may be either

    registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. All other

    trademarks are property of their respective owners.

    http://www.microsoft.com/about/legal/permissions/http://www.microsoft.com/about/legal/permissions/http://www.microsoft.com/about/legal/permissions/http://www.microsoft.com/about/legal/permissions/http://www.microsoft.com/about/legal/permissions/

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    4/360

     

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    5/360

    Page 4 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Contents

    LESSON 1 DEMONSTRATION : INTRODUCTION ............................................................................................... 9 

    HOW TO USE THE EXAMPLES IN THIS WORKSHOP  ............................................................................................ 10 

    EXERCISE 1:  WINDOWS POWERSHELL  ......................................................................................................... 14 

    EXERCISE 2: THE POWERSHELL INTEGRATED SCRIPTING ENVIRONMENT (ISE) ........................................................ 17 

    EXERCISE 3: POWERSHELL V2.0 INSTALLATION AND PREREQUISITES  .................................................................... 20 

    EXERCISE 4: BASIC POWERSHELL COMMANDS  ............................................................................................... 23 

    EXERCISE 5: RUNNING EXTERNAL COMMANDS  ............................................................................................... 26 

    EXERCISE 6: LIST POWERSHELL COMMANDS  .................................................................................................. 28 

    EXERCISE 7: GETTING HELP WITH POWERSHELL.............................................................................................. 30 

    EXERCISE 8: EXPLORE COMMAND HISTORY  ................................................................................................... 32 

    LESSON 1 HANDS-ON : INTRODUCTION ........................................................................................................ 34 

    EXERCISE 1: CREATE A TRANSCRIPT OF COMMANDS  ........................................................................................ 35 EXERCISE 2: USING THE MOST COMMON COMMANDS..................................................................................... 37 

    EXERCISE 3: RUN MULTIPLE COMMANDS  ..................................................................................................... 39 

    LESSON 2 DEMONSTRATION : COMMANDS AND OBJECTS ............................................................................ 41 

    EXERCISE 1: POWERSHELL COMMANDS  ........................................................................................................ 42 

    EXERCISE 2: COMMAND ALIASES  ................................................................................................................ 47 

    EXERCISE 3: THE OBJECT-BASED SHELL  ......................................................................................................... 49 

    EXERCISE 4: THE .NET OBJECT MODEL  ........................................................................................................ 56 

    LESSON 2 HANDS-ON : COMMANDS AND OBJECTS ....................................................................................... 60 

    EXERCISE 1: POWERSHELL COMMANDS  ........................................................................................................ 61 

    EXERCISE 2: DISCOVERING OBJECT MEMBERS  ................................................................................................ 65 

    EXERCISE 3: CREATING OBJECT INSTANCES  .................................................................................................... 68 

    LESSON 3 DEMONSTRATION : PIPELINE ........................................................................................................ 72 

    EXERCISE 1: UNDERSTAND THE FUNDAMENTAL OPERATORS  .............................................................................. 73 

    EXERCISE 2: UNDERSTAND PIPELINE USAGE, SYNTAX, AND THE PIPELINE VARIABLE  ................................................... 78 

    EXERCISE 3: FILTERING, SORTING, AND GROUPING DATA  .................................................................................. 82 

    EXERCISE 4: PIPELINE INPUT AND OUTPUT  .................................................................................................... 87 

    LESSON 3 HANDS-ON : PIPELINE ................................................................................................................... 91 

    EXERCISE 1: POWERSHELL OPERATORS  ........................................................................................................ 92 

    EXERCISE 2: THE POWERSHELL PIPELINE  ....................................................................................................... 94 

    EXERCISE 3: FILTER AND SORT WITH THE PIPELINE  ........................................................................................... 98 

    LESSON 4 DEMONSTRATION : PROVIDERS .................................................................................................. 103 

    EXERCISE 1: PROVIDER INTRODUCTION........................................................................................................104  

    EXERCISE 2: PROVIDER RELATED CMDLETS AND OPERATIONS............................................................................108  

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    6/360

    Page 5 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    EXERCISE 3: SINGLE LEVEL PROVIDERS  ........................................................................................................115 

    EXERCISE 4: MULTIPLE LEVEL PROVIDERS  .....................................................................................................120 

    LESSON 4 HANDS-ON : PROVIDERS ............................................................................................................. 129 

    EXERCISE 1: WORK WITH ENVIRONMENT PROVIDER  .......................................................................................130 

    EXERCISE 2: WORK WITH THE CERTIFICATE PROVIDER  .....................................................................................133 EXERCISE 3: WORKING WITH THE REGISTRY PROVIDER  ....................................................................................136 

    LESSON 5 DEMONSTRATION : VARIABLES AND TYPE FUNDAMENTALS ....................................................... 139 

    EXERCISE 1: WORKING WITH VARIABLES  ......................................................................................................140 

    EXERCISE 2: BASIC VARIABLE TYPES IN POWERSHELL  ......................................................................................145 

    EXERCISE 3: WORKING WITH ARRAYS  .........................................................................................................153 

    EXERCISE 4: WORKING WITH HASH TABLES  ..................................................................................................157 

    EXERCISE 5: WORKING WITH CONSOLE INPUT-OUTPUT  ...................................................................................160 

    EXERCISE 6: WORKING WITH FILES  .............................................................................................................162 

    EXERCISE 7: POWERSHELL ERROR OBJECT  ....................................................................................................166 

    LESSON 5 HANDS-ON : VARIABLES AND TYPE FUNDAMENTALS .................................................................. 171 

    EXERCISE 1: WORKING WITH VARIABLES  ......................................................................................................172 

    EXERCISE 2: WORKING WITH ARRAYS  .........................................................................................................174 

    EXERCISE 3: WORK WITH HASH TABLES  .......................................................................................................176 

    EXERCISE 4: WORK WITH HASH TABLES AND CALCULATED OBJECT PROPERTIES  .....................................................178 

    LESSON 6 DEMONSTRATION : SCRIPTING ................................................................................................... 181 

    EXERCISE 1: SCRIPTING IN POWERSHELL  ......................................................................................................183 

    EXERCISE 2: ITERATION STATEMENTS  ..........................................................................................................189 

    EXERCISE 3: FLOW CONTROL STATEMENTS   ...................................................................................................193 EXERCISE 4: OTHER STATEMENTS  ..............................................................................................................196 

    EXERCISE 5: FUNCTIONS  ..........................................................................................................................200 

    EXERCISE 6: PROFILES  .............................................................................................................................206 

    EXERCISE 7: DOT SOURCING AND SCRIPT LIBRARIES  ........................................................................................209 

    LESSON 6 HANDS-ON : SCRIPTING .............................................................................................................. 211 

    EXERCISE 1: CREATE POWERSHELL SCRIPTS  ..................................................................................................212 

    EXERCISE 2: CREATE FUNCTIONS IN SCRIPTS  .................................................................................................213 

    EXERCISE 3: CREATE POWERSHELL PROFILES  ................................................................................................214 

    EXERCISE 4: EXPLORE ITERATION STATEMENTS IN POWERSHELL  ........................................................................215 

    LESSON 7 DEMONSTRATION : ACTIVE DIRECTORY ADMINISTRATION (ADSI) .............................................. 219 

    EXERCISE 1: ACTIVE DIRECTORY FUNDAMENTALS  ...........................................................................................220 

    EXERCISE 2: SEARCHING ACTIVE DIRECTORY  .................................................................................................224 

    EXERCISE 3: THE DIRECTORY OBJECT LIFECYCLE  .............................................................................................230 

    LESSON 7 HANDS-ON : ACTIVE DIRECTORY ADMINISTRATION (ADSI) ......................................................... 239 

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    7/360

    Page 6 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    EXERCISE 1: CREATE MULTIPLE USERS  ........................................................................................................240 

    LESSON 8 DEMONSTRATION : ACTIVE DIRECTORY ADMINISTRATION (CMDLETS) ....................................... 244 

    EXERCISE 1: USING THE ACTIVE DIRECTORY MODULE  .....................................................................................245 

    EXERCISE 2: USING THE ACTIVE DIRECTORY PROVIDER  ....................................................................................247 

    EXERCISE 3: CMDLETS AND IDENTITY...........................................................................................................249  EXERCISE 4: SEARCHING ACTIVE DIRECTORY USING CMDLETS  ............................................................................252 

    EXERCISE 5: CREATING ACTIVE DIRECTORY OBJECTS USING CMDLETS  ..................................................................254 

    EXERCISE 6: MODIFYING OBJECTS USING CMDLETS  ........................................................................................256 

    EXERCISE 7: POWERSHELL CREDENTIAL OBJECTS AND USING ALTERNATIVE CREDENTIALS FOR CMDLETS ............. .........257 

    LESSON 8 HANDS-ON : ACTIVE DIRECTORY ADMINISTRATION (CMDLETS) .................................................. 258 

    EXERCISE 1:  CREATING MULTIPLE USERS IN AN ORGANIZATIONAL UNIT  ..............................................................259 

    EXERCISE 2: MODIFYING AD OBJECTS VIA CMDLETS  .......................................................................................262 

    EXERCISE 3: SEARCHING AD OBJECTS VIA CMDLETS  ........................................................................................264 

    LESSON 9 DEMONSTRATION : WINDOWS MANAGEMENT INSTRUMENTATION (WMI) ............................... 267 

    EXERCISE 1: INTRODUCTION TO WMI .........................................................................................................268 

    EXERCISE 2: WMI CLASSES AND Q UERIES  ....................................................................................................272 

    EXERCISE 3: WMI REMOTING AND METHOD EXECUTION  ................................................................................281 

    EXERCISE 4: COMMON WMI CLASSES USED  ................................................................................................286 

    LESSON 9 HANDS-ON : WINDOWS MANAGEMENT INSTRUMENTATION (WMI) .......................................... 290 

    EXERCISE 1: WMI CLASSES AND Q UERIES  ....................................................................................................291 

    EXERCISE 2: BASIC FILTERING  ....................................................................................................................293 

    EXERCISE 3: WMI METHOD EXECUTION  .....................................................................................................296 

    LESSON 10 DEMONSTRATION : REGISTRY, EVENT LOG AND ACL MANAGEMENT ........................................ 301 

    EXERCISE 1: USING THE REGISTRY PROVIDER  ................................................................................................302 

    EXERCISE 2: REMOTE REGISTRY MANAGEMENT  .............................................................................................310 

    EXERCISE 3: EVENTLOG CMDLETS...............................................................................................................315  

    EXERCISE 4: FILE AND FOLDER ACL MANAGEMENT  ........................................................................................320 

    LESSON 10 HANDS-ON : REGISTRY, EVENT LOG AND ACL MANAGEMENT ................................................... 322 

    EXERCISE 1: READING REGISTRY INFORMATION FROM A REMOTE HOST  ...............................................................323 

    EXERCISE 2: SEARCHING EVENT LOGS FOR EVENTS  .........................................................................................326 

    EXERCISE 3: FILE AND FOLDER ACL MANAGEMENT  ........................................................................................330 

    LESSON 11 DEMONSTRATION : REMOTING ................................................................................................. 334 

    EXERCISE 1: REMOTE MANAGEMENT WITHOUT POWERSHELL REMOTING  ............................................................335 

    EXERCISE 2: ENABLE POWERSHELL REMOTING  ..............................................................................................338 

    EXERCISE 3: ENABLE POWERSHELL REMOTING WITH THE ACTIVE DIRECTORY GROUP POLICY OBJECT (GPO) ................340 

    EXERCISE 4: EXECUTE A SINGLE REMOTE COMMAND OR SCRIPT  ........................................................................342 

    EXERCISE 5: CREATE A PERSISTENT SESSION TO EXECUTE A SERIES OF REMOTE COMMANDS  .....................................345 

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    8/360

    Page 7 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    EXERCISE 6: CREATE AN INTERACTIVE SESSION WITH A REMOTE MACHINE  ...........................................................348 

    EXERCISE 7: CREATE A SESSION CONFIGURATION  ...........................................................................................350 

    LESSON 11 HANDS-ON : REMOTING ............................................................................................................ 354 

    EXERCISE 1: EXECUTE REMOTE COMMANDS  .................................................................................................355 

    EXERCISE 2: EXECUTE COMMANDS VIA SESSIONS  ...........................................................................................357 EXERCISE 3: INTERACTIVE REMOTE CONSOLE  ................................................................................................359 

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    9/360

    Page 8 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    PowerShell for the IT

    Administrator, Part 1 

    Lesson 1: PowerShell Introduction 

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    10/360

    Page 9 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Lesson 1 Demonstration : Introduction 

    IntroductionThe way this lesson is presented is a little different to what you might be used to. You would

    have noticed that there is no workbook and that we are starting with the lab manual. What we

    are going to do is walk through some examples as an instructor led lab format and then you

    will complete some lab exercises by yourself.

    ObjectivesAfter completing this lab, you will be able to:

      Understand the basic use and capabilities of Windows PowerShell

      Meet the pre-requisites and install Windows PowerShell

      Run basic PowerShell commands

      Use PowerShell help to get help about PowerShell commands

    PrerequisitesTo complete this lab, you need:

      A Windows 7 workstation logged onto with administrator credentials. You can logon as

    contoso\administrator. The password is P@ssword 

    Estimated Time to Complete this Lab

    60 minutes

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    11/360

    Page 10 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    How to use the Examples in this Workshop The way with lesson will be presented will be a little different to what you may be used to.

    You would have noticed that there is no workbook and that we are starting with the lab

    manual. What we are going to do is walk through the examples together as an instructor led

    lab format, and then you will do some more lab exercises by yourself. There are three options

    you can choose from for following the examples:

      Manually type in the commands

      Open the command files in the PowerShell Integrated Scripting Environment (ISE) and

    execute each command by selecting each one

      Using the automated start-demo PowerShell script to run each command automatically

    Each option is a matter of preference. The first option will familiarize you with the

    commands. This is the slowest option and you may have problems keeping up with the

    instructor, but this is a great option to run through after the workshop at your own pace to

    consolidate your learning.

    The second and third options are easier as they do not require type each command. However,

    as you are not typing each command you may miss some commands.

    If you use the second option, the command file can be opened and commands can still be

    typed using the command pane. This allows you to run some of the command from the file

    and type others.

    The third option is the easiest option to run the demo commands. However, you cannot enter

    additional commands manually. This can be easily solved by opening a second PowerShell

    console and typing the additional commands.

    Option 1: Run Commands Manually

    This option is to type the command in manually as they appear in the code blocks of each

    example. You can use tab completion in PowerShell to reduce typing time. Also You can use

    the up arrow to select the most recent commands executed if you need to build from the

     previous command.

    Option 2: Use of Command Files and the ISE

    To use the ISE and the command files, change the default view of the ISE to maximize the

    Real Estate screen. To maximize the Real Estate screen,

    1.  Select the Start button to open the PowerShell ISE.

    2.  Type ISE and run the Windows PowerShell ISE.

    By default, it will appear as follows:

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    12/360

    Page 11 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Figure 1

    3.  Click one of the three rightmost buttons in the toolbar to adjust the pane arrangement.

    This makes it easier to follow all examples.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    13/360

    Page 12 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Figure 2

    In this view,

    1.  Open the command file, or files for the lesson.

    2.  Select each line that does not start with #.

    3.  Press F8 or right-click and select Run or click the icon on the toolbar.

    Option 3: Use of Start-Demo Script

    Using the start-demo script is the easiest option. To use this option, your PowerShell console

    should be opened as normal and the location should be changed to C:\Pshell\part1\lesson1  

    for lesson 1.

    1.  To change the location, type:

    set-location c:\pshell\part1\lesson1

    2.   Now type the following command to load the start-demo.ps1 script function into the

    memory:

    . .\start-demo.ps1

    The dot and space before .\start-demo are required.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    14/360

    Page 13 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    3.   Now type the following command to start the demo script:

    Start-demo

    Where is the demo script file for the lesson or exercise.

    Note: To exit the demo script at any time you can use the ctrl + c keys.

    The script will display the last line number used.

    4.  To restart the script type the following command:

    Start-demo

    Where line number is the number of the line to re-start from.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    15/360

    Page 14 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Exercise 1: Windows PowerShell 

    ObjectivesIn this exercise, you will:

      Learn what Windows PowerShell is  Open Windows PowerShell and run some basic commands

    ScenarioWelcome to Windows PowerShell.

    Windows PowerShell is both a command shell and a scripting language. A significant

    difference between PowerShell and other shells is that it is an object-oriented shell. This

    aspect of PowerShell will be discussed in detail later.

    The use of Windows PowerShell can be divided into two main areas:

      Running commands from the command shell interactively  Running scripts

    Running scripts will be discussed later. For now, we will discuss running commands in the

    shell. Both categories of use of PowerShell are powerful. Depending on the task being

     performed will determine which category to use. For example, Exchange 2007 or 2010

    administrators will be familiar with running commands in the command shell but they may

    not be familiar with running scripts. We will start with opening PowerShell and running

    commands in the console.

    Task 1: Log on to the VM Environment

    1.  Log on to the Windows 7 client.

    2.  Open Windows PowerShell and run the required commands.

    Task 2: Open Windows PowerShell for the First Time

    There are different methods to open Windows PowerShell. Each method has its own purpose.

    The first one we use is the simplest.

    1.  Open PowerShell.

    2.  On the Windows taskbar, click (PowerShell icon).

    The PowerShell window appears.

    You will notice that it looks different to the standard command prompt. It should have a

     blue background and white text. It will also have the text

    PS C:\users\administrator> PS indicates that you are using PowerShell and not the

    CMD shell.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    16/360

    Page 15 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Task 3: Run the First Command

    1.  Type the following command:

    dir

    You will see that now you have displayed the files and folders that are available from

    your current location. Let us compare the PowerShell display and the CMD shell display.

    2.  Click Start > Run, type cmd and press Enter.

    3.  In the CMD shell, type the following command:

    dir

    4.  Compare the output of PowerShell and the output of the CMD shell.

    You will notice that they are very different. The main advantage of PowerShell is that it

    is easy to change the view.

    Task 4: Change PowerShell View1.  In the PowerShell console, type the following command to display the file system

    objects:

    dir | format-table name, lastwritetime, length

    You will see that we now have the same information but it appears very differently. Now

    let us open PowerShell from the run box. It will look different.

    2.  Click Start > Run, type powershell and press Enter.

    The PowerShell prompt appears, but this time you will see it looking more like the CMD

    shell, as it has the black backgorund and the grey text. Note that it will have the PS

    C:\users\administator> prompt.

    3.  In the PowerShell console, type the following command to display the file system

    objects:

    dir | format-table name, lastwritetime, length

    You will notice that the output is the same as in the blue background window. Now, close

    the black background window and let us run another command in the blue background

    window.

    4.  Type the following command to display processes.

    get-process

    A list of all the processes running on your machine appears. Now, run the same command

    from the command prompt.

    5.  Click Start > Run and type the following command:

    powershell –command get-process

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    17/360

    Page 16 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    You will see the command run and produce the output, but the window closes as soon as

    it finishes running the command. To prevent this, you have the option of keeping the

    window open after you run the command. You will do this now using the noexit option.

     Note that this needs to be before the – command option.

    6.  Click Start > Run and type the following command:

    PowerShell –noexit –command get-process

    You will now see that the command is executed and the window stays open. You can

    explore other options for running PowerShell.exe and commands by typing  PowerShell /?

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    18/360

    Page 17 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Exercise 2: The PowerShell Integrated ScriptingEnvironment (ISE) 

    PowerShell v2.0 comes with its own graphical scripting editor known as the ISE. The ISE is a

    fully integrated scripting environment that both aids in using PowerShell and enhances it. The

    ISE is installed by default on Windows 7 machines, and installed automatically onworkstations as part of the Windows management framework package (that includes

    PowerShell v2). On Windows Server 2008 R2, it is a feature that you have to enable, or select

    to install on other operating systems. Once you have the ISE available then it can be run.

    1.  On your workstation click Start > Run and type PowerShell ISE and press Enter.

    The PowerShell ISE opens and you will see the three parts of it.

    Figure 3

    Script PaneAllows you to create and run scripts. You can open, edit, and run existing scripts in the

    Script Pane.

    Output PaneDisplays the results of the commands and scripts you have run. You can also copy and

    clear the contents in the Output Pane.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    19/360

    Page 18 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Command PaneAllows you to type commands. You can run a one-line command or a multi-line

    command in the Command Pane. Press SHIFT+ENTER  to enter each line of a multiline

    command, and press Enter after the last line to execute the mult-line command. The

     prompt displayed on top of the Command Pane shows the path to the current working

    directory.

    From here, we can run PowerShell commands and view the results.

    Task 1: Run PowerShell

    1.  Click the Command pane and type the following command:

    dir | format-table name, lastwritetime, length

    You will now see in the output pane the results of the command. This is just like having a

    PowerShell console open, but we are running these commands from within the ISE. Now

    will we run some commands from the script pane.

    2.  Click the Script pane and type the following:

    get-process

    You can open the file _ISE-commands.ps1 if you do not want to type the commands.

    3.  On the next line, type the following command:

    dir | format-table name, lastwritetime, length

    4.  On the next line type the following:

    write-host “Hello from PowerShell!” 

    You will notice that the ISE will now color code the different parts of each command.This is to help identify the different parts of each command such as the command part,

    and strings code blocks.

    Task 2: Run All Commands 

    You can now run all commands using the Run button(or press F5) or select the line or part of

    the line to be run. 

    1.  Drag and select the text you want to run.

    2.  Use the Run selection button or press F8 to run the command.

    3.  Select the first line that has get-process and select the text.

    4.  Select the Run selection button or press F8 to run the command.

    You will see that in the output pane, you have the results of the get-process command.

    5.  Select the second line with dir | format-table name, lastwritetime, length and run this

    selection.

    The output for this command appears in the output pane.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    20/360

    Page 19 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    6.  Click the Run button or press F5 to run all the commands.

    You will see the output of all commands.

    7.  Keep the ISE open and run commands in it, or close the ISE and run commands in the

    PowerShell.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    21/360

    Page 20 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Exercise 3: PowerShell V2.0 Installation andprerequisites 

    Before you run PowerShell, you must make sure it is available. PowerShell v2.0 is installed

     by default on Windows 7 and Windows Server 2008 R2. Therefore, for these operating

    systems you do not have to do anything additional to make PowerShell available.

    For operating systems other than Windows 7 and Windows Server 2008 R2 , you will need to

    download and install the Windows Management Framework package. It is available from

    http://support.microsoft.com/default.aspx/kb/968929. This package will install Windows

    PowerShell 2.0, WinRM 2.0, and BITS 4.0. The update can be installed on the following

    operating systems:

      Windows Server 2008 with Service Pack 1

      Windows Server 2008 with Service Pack 2

      Windows Server 2003 with Service Pack 2

      Windows Vista with Service Pack 2

      Windows Vista with Service Pack 1

      Windows XP with Service Pack 3

      Windows Embedded POSReady 2009

      Windows Embedded for Point of Service 1.1

    In addition to the Windows Management Framework package, the .NET framework 3.5 SP1

    is also required on the system to install PowerShell v2.0.

    Although PowerShell v1 is available, there are many new features of PowerShell v2.0 that

    should be considered. PowerShell v1 is available for the following systems:

      Windows Server 2003 with Service Pack 2

      Windows XP with Service Pack 3

    PowerShell v1.0 is installed by default, on Windows Vista and as a feature for Windows

    Server 2008. The requirements for PowerShell v1.0, are that the .NET framework 2.0 is

    installed on the system. At this stage, it is not recommended that PowerShell v1 be installed,

    as v2 has many additional features available.

    By default, all versions of PowerShell will have the execution of scripts disabled. This has to

     be turned on either by the command set-execution policy or via group policy/registry settings.

    For PowerShell v2, PowerShell Remoting is also disabled by default. This has to be enabledwith the command enable-psremoting or via group policy/registry settings.

    Task 1: Determine the PowerShell Version

    There are several ways you can determine the version of PowerShell you are currently

    running, or is available on a machine. We will look at several of these and a few things of

    note.

    http://support.microsoft.com/default.aspx/kb/968929http://support.microsoft.com/default.aspx/kb/968929http://support.microsoft.com/default.aspx/kb/968929

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    22/360

    Page 21 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Use of $host.version1.  In the PowerShell console, type the following command to display the version:

    $host.version

    This will now display the version number of Windows PowerShell. The number to take

    the most note of is the major version number.

    Use of $psversiontable1.  In the PowerShell console, type the following command to view the ps version table:

    $psversiontable

    This table of version numbers not only gives you the version of PowerShell installed but

    also lists the version of other related components. This is useful when you start looking at

    some of the advanced features of PowerShell such as remoting. The number to take

    interested in here is the PSVersion number.

    Use of RegistryYou can also check the version of PowerShell installed on a machine by checking the registry.

    This can be very useful, as it will allow us to check the version of PowerShell installed on a

    remote machine without having to run commands in a PowerShell console.

    1.  In the PowerShell console, type the following command to get the version information

    from the registry:

    get-itemproperty HKLM:\software\microsoft\PowerShell\1\PowerShellEngine

    You will now see the information from the registry key. The properties to take note of are

    the runtimeversion and the powershellversion number.

    You can use the same path on a remote machine to check the PowerShell versioninstalled on the target. This will be discussed later.

    Use of PSSnapinsBy looking at the versions of the PowerShell snapins you can determine the version of

    PowerShell and see the versions of each snapin that makes up the collections of PowerShell

    Cmdlets.

    1.  In the PowerShell console, type the following command to list the PowerShell snap-ins:

    get-pssnapin

    You will now see the current PowerShell snapins and their PowerShell version numbers.

    Points to Note About PowerShell v2.0

    There are a few things to take note of when looking at PowerShell and its version. Several

    things such are registry paths, file extensions and install locations did not change between

    PowerShell v1 and PowerShell v2. These can cause confusion about the version of

    PowerShell that is in use. First to note is that the file extension .PS1 is still the script file

    extension for PowerShell v2.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    23/360

    Page 22 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    The next is the PowerShell install location.

    1.  In the PowerShell console, type the following to set the location to the PowerShell install

    location:

    $pshome

    This variable contains the path to the PowerShell install location. Note that even on a

    machine with PowerShell v2 installed, this still points

    to %systemroot%\ System32\WindowsPowerShell\v1.0.

    The registry path for PowerShell v2 is also HKLM:\software\microsoft\PowerShell\1\. 

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    24/360

    Page 23 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Exercise 4: Basic PowerShell Commands In this exercise, you will run some basic PowerShell commands. PowerShell allows you to

    run both PowerShell commands, many of these called Cmdlets, and external commands such

    as executable. First, you need to create a transcript to record all the commands you type. This

    will log all commands and the output they produce.

    1.  In the PowerShell console, type following command to start a transcript

    start-transcript PowerShell_transcript.txt

    This will now start the transcript recording our commands.

    You will get a quick list of the commands available to run in PowerShell. To do this, use

    the Cmdlet called get-command.

    Transcript commands will not work if you are running the demos from within the ISE

    2.  In the PowerShell console, type the following command to list the commands available.

    get-command

    A list of commands available to you under the current PowerShell console appears.

    However, you will notice that this list contains different types of commands. For now,

    you need only Cmdlets. Use get-command to display only the Cmdlets.

    3.  In the PowerShell console, type the following command to list Cmdlets:

    get-command –commandtype cmdlet

    This will now list just the Cmdlets. You will notice it is a much shorter list than before. In

    fact you can use PowerShell to tell exactly how many Cmdlets there are. You can do thiseasily using two different methods.

    4.  In the PowerShell console, type the following command to count the Cmdlets available:

    get-command –commandtype cmdlet | measure-object

    The count of available Cmdlets appears. By default, in Windows 7 the count should be

    236 at the time of writing. There is another way to do this.

    5.  In the PowerShell console, type the following command to count the Cmdlets available:

    (get-command –commandtype cmdlet).count

    The number of Cmdlets appears. However, this time you will see only the number.

     Now let us run some more commands.

    1.  In the PowerShell console, type the following command to get the current date:

    get-date

    The current date and time appears. You can change what appears by using some

     parameters in the command.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    25/360

    Page 24 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    2.  In the PowerShell console, type the following command to get just the date:

    get-date –displayhint date

    Only the date appears.

    3.  In the PowerShell console, type the following command to get only the current time:

    get-date –displayhint time

    Only the time appears.

    You can also view different time formats.

    1.  In the PowerShell console, type the following to display the short date format:

    get-date –displayhint time –format d

    2.  In the PowerShell console, type the following command to display the long date format:

    get-date –displayhint time –format D

    3.  In the PowerShell console, type the following to display the short time format:

    get-date –displayhint time –format t

    4.  In the PowerShell console, type the following command to display the long time format:

    get-date –displayhint time –format T

    5.  In the PowerShell console, type the following to display the date in a given format:

    get-date –displayhint time –format yyyy/MM/dd

    This will display the date in the given format. Note that this is case sensitive. Note also

    that you should always use ISO8601 for dates rather than a specific locale.

    6.  In the PowerShell console, type the following command to display time in a given

    format:

    get-date –displayhint time –format hh:mm:ss

    This will display the time in the given format. Again, note that this is case sensitive and

    the lower-case ‘mm’ produces minutes, whereas the upper -case ‘MM’ produces the

    month.

    Let us look at something more interesting.

    1.  In the PowerShell console, type the following command to list the current processes:

    get-process

    A list of the currently running processes appears. We can do the same with services.

    2.  In the PowerShell console, type the following command to list the services on the system:

    get-service

    A list of services and their statuses appears. However, you might be interested in only

    one service. You can view this by typing its name after get-service.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    26/360

    Page 25 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    3.  In the PowerShell console, type the following command to get information for just the

     bits service:

    get-service bits

     Now you will see information about only the bits service

    4.  In the PowerShell console, type the following command to generate a random number

     between 1 and 10:

    Get-random –Minimum 1 –Maximum 10

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    27/360

    Page 26 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Exercise 5: Running External Commands The great thing about PowerShell is that you do not have to call the external command with

    anything special to get them to run. You can run the command by typing the name of the

    command and pressing Enter.

    1.  In the PowerShell console, type the following command to get IP configuration

    information:

    ipconfig

    You will now see that you have the same output that you would have had if you run the

    command in the CMD shell.

    2.  In the PowerShell console, type the following command to get all IP configuration

    information:

    ipconfig /all

    You will again see the same output as the CMDshell.

    3.  In the PowerShell console, type the following command to get system information:

    systeminfo

    This will now list a summary of the system information of the current machine.

    What may be useful is to write this information to a file. Just like the CMD shell, you can

    use the > and >> symbols to redirect the output. > will overwrite the output, whereas >> 

    will append the output.

    4.  In the PowerShell console, type the following command to redirect output to a file:

    systeminfo >systeminfo.txt

    This will now write that information to a text file for us. To view it lets use notepad

    5.  In the PowerShell console, type the following command to open the text file in notepad:

    notepad systeminfo.txt

    You will now see the information from the command in the text file. This not only works

    for external commands, but also for Cmdlets.

    6.  In the PowerShell console, type the following command to redirect the Cmdlet output to

    a file:

    get-service > services.txt

    7.  In the PowerShell console, type the following command to read the file:

    notepad services.txt

    The information about services appears.

    You can also get PowerShell to run multiple commands in a sequence. This can be useful as

    you can put multiple commands in one line and PowerShell will run these in a sequence. This

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    28/360

    Page 27 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    is especially useful if you have a command that takes a while to run and you do not want to

    wait until it is finished. To do this, use the semicolon character. This is the statement

    terminator for PowerShell.

    1.  In the PowerShell console, type the following command to run multiple commands, add

    multiple items to a text file and open it in Notepad:

    ipconfig /all > systemconfig.txt; systeminfo >>systemconfig.txt ; get-service

    >>systemconfig.txt; notepad systemconfig.txt

    The three commands to get information about the system will now run, and each of these

    will put information into the systemconfig.txt. Finally, open Notepad to view this

    information.

    The $lastexitcode variable can be very useful when working with external commands. This

    variable contains the exit code or errorlevel value from the external command. In general, an

    exit code of zero is a success and anything non-zero is considered an error of some type.

    However, this may not be true depending on the program you are running. Let us look at a

    simple example using the ping command.

    1.  In the PowerShell console, type the following command to ping the local machine:

    ping localhost

    2.  In the PowerShell console, type the following command to check the status of the

     previous command:

    $lastexitcode

    You will now see that the value of the $lastexitcode is now zero. This is because we were

    able to ping the localhost.

    3.  In the PowerShell console, type the following command to ping a non-existing host:

    ping fakeserver

    4.  In the PowerShell console, type the following command to check the status of the

     previous command:

    $lastexitcode

    You will now see that the value of the $lastexitcode is now one. This is because, you

    were unable to ping the fakeserver host, as it does not exist.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    29/360

    Page 28 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Exercise 6: List PowerShell Commands You looked at listing available PowerShell commands quickly a little while ago. But it is

    worth spending a little more time on this as it is difficult to remember around 236 or more

    commands. There will be a number of Cmdlets that you will use repeatedly, and some you

    may never use. To help you find Cmdlets in PowerShell we have the get-command Cmdlet.This can be used in a number of ways to find the commands that you are interested in.

    1.  In the PowerShell console, type the following command to list the commands available:

    get-command

    You will now see a list of Cmdlets, functions and alias. This does work a little differently

    in PowerShell v1 and v2. In v1 you would have only seen Cmdlets by default.

    2.  In the PowerShell console, type the following command to list just Cmdlets:

    get-command –commandtype cmdlet

    You will now see a list of just the Cmdlets available.

     Now, let us look for commands that start with get.

    3.  In the PowerShell console, type the following command to list all the command that start

    with get:

    get-command get*

    A list of commands that start with get appears. You will notice that most of these are

    Cmdlets. However, there are some external commands such as getmac.exe and

    gettingstarted.exe listed. This is because, these are commands that PowerShell can run.

    You will notice that these external commands are listed as the type of application. This

    tells you that it is an external command. If you combine the last two options, -

    commandtype and get* you will see only the Cmdlets.

    4.  In the PowerShell console, type the following command to list just Cmdlets that start

    with get:

    get-command –commandtype cmdlet get*

     Now, you will see just Cmdlets that start with get. There is an easier way to do this.

    You can use the -verb option to look for Cmdlets or functions.

    5.  In the PowerShell console, type the following command to list Cmdlets with the verb get:

    get-command –verb get

    You will now see any Cmdlets and functions that have the verb get in their name. We

    will go into more details about the naming of Cmdlets later, but you will find that the

    names of the Cmdlets follow verb-noun in their names. This makes it easy to know what

    a Cmdlet might do, but also makes is easy to find the Cmdlets.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    30/360

    Page 29 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    6.  In the PowerShell console, type the following command to list Cmdlets with the noun

    service:

    get-command –noun service

    7.  You will now see all the Cmdlets that are related to services. We can also do this with a

    wildcard search.

    8.  In the PowerShell console, type the following command to get all commands that end

    with service:

    get-command *service

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    31/360

    Page 30 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Exercise 7: Getting Help with PowerShell One of the useful things with PowerShell is that it has a great built-in help system. All

    Cmdlets have a help topic and these topics have both, a consistent format and a consistent

    method for accessing it. This is all done with the Cmdlet get-help. In addition, to having

    help information for Cmdlets, there are also additional help topics covering PowerShellconcepts,

    1.  In the PowerShell console, type the following command to display the help details:

    get-help

    The help information for the get-help Cmdlet appears. You will notice that information

    follows a particular format. The good thing is that this format is the same for all Cmdlets.

    2.  In the PowerShell console, type the following command to get help on the get-command

    Cmdlet:

    get-help get-command

    The help information for the get-command Cmdlet appears. You can expand on this by

    using two options.

    3.  In the PowerShell console, type the following command to get a detailed help on get-

    command:

    get-help get-command –detailed

    Some of the same information as before appears on top of the display. However, as you

    scroll down you will find more information such as the parameter descriptions and some

    examples. Let us do this again with a different Cmdlet.

    4.  In the PowerShell console, type the following to get detailed help on get-service Cmdlet:

    get-help get-service –detailed

    The detailed help information for the get-service Cmdlet appears. There is another option

    that is useful, the  – full option. This will display all the help information for the Cmdlet.

    5.  In the PowerShell console, type the following to get all help on get-service Cmdlet:

    get-help get-service –full

    The full help information for that Cmdlet appears.

    Another option that can be a great way to jog your memory about how a Cmdlet works is the

     – examples option. This will display some examples of how to use the Cmdlet.

    6.  In the PowerShell console, type the following command to get just examples of the get-

    service Cmdlet:

    get-help get-service –examples

    In addition to getting help on Cmdlets, there are many other help topics available. To see

    them we will use the following command.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    32/360

    Page 31 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    7.  In the PowerShell console, type the following command to list all help topics:

    get-help *

    The full list of help topics available appears. You will see the alias, the functions,

    Cmdlets and finally the helpfile topics. You will notice that all the helpfile topics all have

    the name about_ this allows us to look just at them.

    8.  In the PowerShell console, type the following command to list just the about topics:

    get-help about

    Only the information about helpfile topics appears.

    9.  In the PowerShell console, type the following command to display the wildcards help

    topic:

    get-help about_wildcards

    The information about PowerShell wildcards appears. Note that there is no  – full or  – 

    detailed option with the about topics.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    33/360

    Page 32 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Exercise 8: Explore Command History One of the things that can help a lot in the early stages of learning PowerShell is to look at the

    history of the commands that you have run. This can be done in a few different methods, but

    we will look at the two main methods.

    The first is using the get-history Cmdlet to display the previous commands run.

    1.  In the PowerShell console, type the following command to view the command history:

    get-history

    The previous commands run up to a default maximum of 64 commands. You will notice

    that each command has an ID associated with it. This ID can be used to re-run a

     particular command.

    2.  In the list of commands, locate the ID for the get-help and get-service commands.

    3.  In the PowerShell console, type the following command to invoke the command from

    history:

    invoke-history –id

    The command executes again. This can be useful when you have complicated lines of

    code and you do not want to use the up arrow to go through all commands again.

    The previous commands can also be exported and later imported to use again.

    1.  In the PowerShell console, type the following command to export the history to a csv file:

    get-history | export-csv myhistory.csv

    The command history is exported to a CSV file.

    2.  Open a new PowerShell console and type the following command to view the current

    history:

    get-history

    As this is a new PowerShell session, you will see that there currently are no commands in

    the history. You can now import the commands from the CSV file we created to include

    some history commands that can be invoked.

    3.  In the new PowerShell console, type the following command to import the history from

    the other session:

    import-csv myhistory.csv | add-history

    4.  In the new PowerShell console, type the following command to view the current history:

    get-history

    You will now see that there are commands loaded into the history and can be invoked.

     Notice that the IDs will not be the same as the session that we exported the information

    from.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    34/360

    Page 33 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    One thing to note is that by default, you will only see the last 64 commands. This number

    is defined by the variable $MaximumHistoryCount.

    To increase the maximum commands:

    1.  In the original PowerShell console, type the following command to increase the

    maximum command history count:

    $MaximumHistoryCount = 100

    This will allow you to look at up to 100 previous commands. You can also specify  – 

    count 100 for get history to display the 100 commands. By default, you will only get 32

    displayed.

    2.  In the PowerShell console, type the following command to get the last 100 commands:

    get-history –count 100

    There is another way of viewing the previous commands run. However, this will also

    show the output produced from each of these.If you remember, earlier in the lesson, we created a transcript. This will have recorded all the

     previous commands that we have run. So let us stop and view the contents of that transcript.

    1.  In the PowerShell console, type the following command to stop the transcript:

    Stop-Transcript

    This will stop recording of the session and tell you where the output file is.

     Again, remember here (and moving forward through the additional demonstrations) thattranscript commands will fail if being run through the ISE.

    2.  In the PowerShell console, type the following to read the transcript:

    notepad

    By reading the file, you will see the history of all the commands you have run. This file

    should be kept for later use, as it can be useful to look at the commands you have run.

    It is suggested that at least for the next few lessons, start a transcript for each lesson so that

    you can review those later.

    In addition to the history Cmdlets, you can also use the up arrow to see the previous

    commands run. To run the same command again, press Enter. You can also use the up arrow

    to bring up a previous command and modify it. The drawback here is that if you need to go

     back several commands, or you need to re-run multiple commands, the order of these

    commands will change each time a new command is executed.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    35/360

    Page 34 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Lesson 1 Hands-On : Introduction 

    ObjectivesThe objectives for this lab are:

      To create transcripts of PowerShell commands

      To practice using the top 3 Cmdlets (i.e. get-help, get-command and get-member)

      To execute multiple commands in a single line

    Prerequisites

      The lab requires a Windows 7 client running in a domain environment.  

    Estimated time to complete this lab

    30 minutes

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    36/360

    Page 35 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Exercise 1: Create a Transcript of Commands 

    ObjectivesIn this exercise, you will:

      Practice creating transcripts of PowerShell commands

    Task 1: Log on to the VM environment

    1.  Log on to Windows 7 Enterprise client as Contoso\Administrator with the password,

    P@ssword 

    Task 2: Open Windows PowerShell Session

    On the Windows taskbar, click .

    The PowerShell window appears.

    Task 3: Record commands in a text file

    1.  Type the following in the PowerShell console. This will NOT work in the PowerShell

    ISE as transcripts are not supported under the ISE Host.

    new-item -path $home\documents -name transcripts -type directory

    This will create a new directory for storing transcript files.

    2.  Type the following to set the transcript variable to a desired location for storing transcript

    files

    $global:transcript=”$home \documents\transcripts\$((get-

    date).tostring("yyyyMMdd HHmmss")).txt”  

    Note: This global variable will be lost when the PowerShell session is terminated. Thevariable assignment can be added to a Profile if it is required to persist betweensessions.

    Note also that you should always use ISO8601 for dates rather than a specific localeas shown in tostring("yyyyMMddHHmmss")) above.

    3.  Type the following to start the recording part of this PowerShell session in a text file.

    start-transcript.

    4.  Enter some commands, for example:

    Get-command

    Get-service

    Get-process

    Get-help

    5.  Type the following to stop recording this PowerShell session.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    37/360

    Page 36 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    stop-transcript 

     Note that the file path and name appears on the console.

    6.  Type the following to view the transcript file name.

    notepad $transcript 

    7.  Type the following to start recording again.

    start-transcript  

    8.  Enter some commands, for example:

    Get-command -commandtype Cmdlet

    9.  Type the following to stop recording this PowerShell session.

    stop-transcript 

     Note that the file path and name appears on the console.

    10. Type the following to view the transcript file name.

    notepad $transcript

     Note that the same file is used and the previous content was overwritten.

    11. Type the following to start recording again

    start-transcript -append 

    12. Enter some commands, for example:

    Get-command -commandtype alias

    13. Type the following to stop recording this PowerShell session.

    stop-transcript 

     Note that the file path and name appears on the console.

    14. Type the following to view the transcript file name.

    notepad $transcript

     Note that the same file is used and the previous content is still available.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    38/360

    Page 37 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Exercise 2: Using the Most Common Commands 

    ObjectivesIn this exercise, you will:

      Practice using the most common and useful PowerShell commands

    Task 1: Log on to the VM environment

    1.  Log on to Windows 7 Enterprise client as Contoso\Administrator with the password,

    P@ssword 

    Task 2: Open Windows PowerShell Session

    On the Windows taskbar, click .

    The PowerShell window opens.

    Task 3: Get-Help, Get-Command and Get-Member1.  Type the following to view the full help available on using the get-help Cmdlet.

    2.  get-help get-help -full Type the following to view help for the get-command Cmdlet.

    get-help get-command 

    3.  Type the following to view help for the get-member Cmdlet.

    get-help get-member 

    4.  Type the following to return the methods and properties that are available for the output

    of the (get-help get-command) command.

    (get-help get-command) | get-member

    5.  Type the following to return only the properties that are available for the output of the

    (get-help get-command) command.

    (get-help get-command) | get-member -membertype noteproperty

    6.  Type the following to return only the methods that are available for the output of the (get-

    help get-command) command.

    (get-help get-command) | get-member -membertype method

    7.  Type the following to display the command syntax only.

    (get-help get-command).syntax

    8.  Type the following to display all the parameters of the command.

    (get-help get-command).parameters

    9.  Type the following to view the static members of the datetime type.

    [datetime] | get-member -static

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    39/360

    Page 38 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    A.  Type the following to view the current date and time.

    [datetime]::now

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    40/360

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    41/360

    Page 40 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    PowerShell for the IT

    Administrator, Part 1 

    Lesson 2: PowerShell Commands 

    Student Lab Manual 

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    42/360

    Page 41 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Lesson 2 Demonstration : Commands and objects 

    IntroductionThis lab introduces PowerShell commands, known as cmdlets. You will learn about cmdlet

    usage, discovery and syntax for a complete understanding of their importance. You will also

    understand the concept of objects, which is fundamental to using PowerShell effectively.

    ObjectivesAfter completing this lab, you will be able to:

      Explore command discovery, syntax and usage

      Leverage command help topics

      Discover & create command aliases

      Explain the usage of classes, objects & various object models in PowerShell

    Pre-requisites

    To complete this lab, you need:

      A Windows 7 workstation logged onto with administrator credentials

    Estimated time to complete this lab60 minutes

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    43/360

    Page 42 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Exercise 1: PowerShell Commands 

    ObjectivesIn this exercise, you will:

      Learn command syntax

      Learn command usage

      Find command help

      Discover commands and their grouping

    ScenarioPowerShell consists of four types of commands:

      Cmdlets

      Functions

      Scripts

      External commands

    In this exercise, we will focus on using the first type of commands, known as Cmdlets

    (pronounced ‘command-lets’).

    Cmdlets are commands ‘built-in’ to PowerShell. They are written in a .NET language (C#,

    VB.NET, F#, etc.) and compiled into a dynamic link library (.DLL) file.

    236 Cmdlets are available by default in PowerShell v2.0 and cover a wide range of uses, from

    interacting with the file system to listing event log and service information.

    Although out-of-scope for this lesson, it is worth noting that new Cmdlets authored and

    compiled by a developer can be loaded into the PowerShell process, alongside the default

    Cmdlets.

    Task 1: Understand the Command Syntax

    Commands have a verb-noun naming convention, where the verb describes the action to take

    on the noun. Nouns are always named in a singular way ( Process rather than Processes). It is

    also worth noting that PowerShell is not  case sensitive.

    Command names are followed by a number of hyphen-prefixed parameter names which may

     be paired with an argument and are known as named  parameters. All parameters and theirarguments are separated by one or more space characters. Parameter arguments can also be

    inferred by their position in the command without specifying the parameter name. These are

    known as positional  arguments.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    44/360

    Page 43 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

     Name Switch Parameter Parameter with argument Positional argument

    Verb-Noun  –  parameter1  –  parameter2

    1.  Open the PowerShell console or ISE and type the following command:

    Get-Process

    This command will return a list of all processes running on the local machine.

    2.  To limit the number of processes returned, add a parameter and an argument. The

    following example uses the Get-Process Cmdlet’s –  Name parameter.

    Get-Process -Name explorer

    This will return a single line of information about the “explorer” process running on the

    local machine.

    3.  Parameter names can also be shortened by abbreviating them to a unique value.

    Get-Process -Na explorer

    4.   Next, try typing the same command using PowerShell’s tab-completion feature. Type the

    following:

    Get-

    Then, press the Tab key until the full command name appears. Pressing SHIFT+TAB 

    moves backwards through the list.

    5.  This feature also works with parameter names. Type a space character and a hyphen 

    character. Pressing the Tab key will cycle through all the parameters for a particular

    command. Again, pressing Shift+Tab moves backwards through the list.

    Get-Process -

    6.  Certain parameter names can be omitted entirely with the command still working as

    expected. Below, the argument explorer  is associated with the –  Name parameter by its

     position in the command. Remember that the arguments have to be passed in the default

    order specified by the Cmdlet.

    Get-Process explorer

    7.  Many parameters also accept a list of arguments, allowing more than one value to be

     bound to a parameter. In PowerShell, lists can be specified by separating each item with a

    comma.

    Get-Process –Name explorer,system,wmiprvse

    8.   Now add a parameter that does not require an argument. Such parameters are called

     switch parameters, since they change the command's behavior by enabling a feature

    within it.

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    45/360

    Page 44 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    For example, the module switch parameter of the Get-Process Cmdlet turns on a feature

    that lists all the .dll files loaded by the returned process.

    Get-Process -Name explorer -Module

    9.  If you omit any required parameters, PowerShell will prompt you to supply the required

    arguments automatically. In the example below, you are prompted for the -Path and -Type parameter arguments to successfully create a new folder on the local C: volume.

    PS C:\Users\Administrator> New-Item

    cmdlet New-Item at command pipeline position 1

    Supply values for the following parameters:

    Path[0]: c:\test

    Path[1]:

    Type: directory

    Directory: C:\

    Mode LastWriteTime Length Name

    ---- ------------- ------ ----

    d---- 11/08/2011 10:00 PM test

    Task 2: Search for Commands (Get-Command)

    In this task, you will understand how to search for Cmdlets. To do this, use a Cmdlet

    designed specifically for this purpose. The Get-Command  Cmdlet returns a list of commands.

    1.  Open the PowerShell console or ISE.

    2.  Type the following command and press Enter:

    Get-Command

    A list of Cmdlets, Functions and Aliases will be displayed, since all command types are

    listed by default.

    3.  If you were searching for a command containing a particular string you can enclose it in

    wildcard (*) characters.

    Get-Command *item*

    4.  Get-Command also has – verb and – noun parameters which allow you to limit the search

    to commands matching the verb and/or noun part of the name. Below we list all the

    Cmdlets where the word on the left-hand side of the hyphen, the verb, matches the string

    “get”. 

    Get-Command –verb get

    Wildcard characters can also be used in parameter arguments.

    5.  List all the commands where the noun part of the name starts with the string ‘object’.

    Get-Command –noun object

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    46/360

    Page 45 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    6.  List all commands where the verb part of the name ends with the letter ‘w’.

    Get-Command –verb *w

    Task 3: Get Command Help (Get-Help)

    PowerShell has an extensive help system accessible through the Get-Help Cmdlet. The

    Cmdlet can return the syntax, description, parameters and examples for any command.

    1.  To use this Cmdlet, type the name of the command you want help for as the first

    argument.

    Get-Help New-Item

    This use of the command displays basic help information. Get-Help also has a number of

    switch parameters that control the amount of help information returned. The -full  

     parameter returns all information about a particular command.

    Get-Help Get-Service -full

    The -examples parameter  lists different ways in which a command can be used.

    Get-Help New-Item -examples

    The -detailed  parameter adds examples and descriptions to the basic help.

    Get-Help New-Item -detailed

    As with all commands, wildcard characters can be used to return help for multiple

    matches. If only one command matches, PowerShell returns the help for that command. If

    not, a list of matching help topics is displayed.

    Get-Help new*

    Another useful switch parameter for this Cmdlet is -Online. If you have internet access,

    this parameter will open the TechNet Windows PowerShell command Help Topics

    documentation for the specified command. http://technet.microsoft.com/en-

    us/library/dd347701.aspx. 

    Get-Help New-Item -Online

    2.  Get-Help can also be used to display conceptual help about the PowerShell Language.

    This is accessed by using the argument about_*, which will list all of the conceptual help

    topics.

    Get-Help about_*

    You can then choose the topic and use its full name to list the entire help file.

    Get-Help about_Command_Syntax

    http://technet.microsoft.com/en-us/library/dd347701.aspxhttp://technet.microsoft.com/en-us/library/dd347701.aspxhttp://technet.microsoft.com/en-us/library/dd347701.aspxhttp://technet.microsoft.com/en-us/library/dd347701.aspxhttp://technet.microsoft.com/en-us/library/dd347701.aspxhttp://technet.microsoft.com/en-us/library/dd347701.aspx

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    47/360

    Page 46 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Note: A very useful piece of information returned by all the Get-Help switch parametersis the command syntax. This has a special format that is easy to understand, once youknow how to interpret it!

    -  [- ]  [-] [-]  

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    48/360

    Page 47 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Exercise 2: Command Aliases In the previous exercise, the full verb-noun name was used when calling commands. Using

    aliases, PowerShell provides the ability to create alternate names for any command.

    Note: Aliases execute the underlying command using a different name.

    There are two types of built-in aliases: Transitional and Convenience.

    Transitional aliases: Transitional aliases were created to assist users migrating from

    cmd.exe or UNIX/Linux shells to PowerShell. For example, the cmd.exe dir command lists

    files and folders in the current directory. In UNIX/Linux shells, the same operation is

    achieved using the ls command. PowerShell implements two transitional aliases for its

    equivalent directory listing Cmdlet, Get-ChildItem. The following commands both execute 

    Get-ChildItem. 

    dirls

    Convenience aliases: Convenience aliases are, as the name suggests, for convenience. For

    example, Get-ChildItem has a convenience alias of gci, which saves the time required to type

    the complete command name.

    gci

    Note: By default, PowerShell 2.0 has 137 aliases. The majority can be re-assigned topoint to a different command or deleted entirely, although they will be re-populatedwhen a new console or ISE session is established.

    Task 1: Find Different Alias Commands

    1.  Find all the ‘*-Alias’ commands using either of the methods below:

    Get-Command –Name *alias

    Get-Command –Noun alias

    You will see five Cmdlets that can be used to manipulate aliases. The most useful of

    which are Get-Alias , New-Alias and Set-Alias.

    a.  The Get-Alias Cmdlet is used to list all aliases.

    Get-Alias

     b.  New-Alias allows you to create new aliases for a Cmdlet, Function or executable file.

    The -Name parameter argument specifies the name of the new alias and the -Value 

     parameter specifies the command to alias.

    New-Alias –Name gp –Value Get-Process

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    49/360

    Page 48 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    c.  The Set-Alias Cmdlet can point an existing alias to a different command.

    Set-Alias –Name gp –Value Get-PSProvider

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    50/360

    Page 49 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Exercise 3: The Object-based Shell Traditionally, the Windows command console and UNIX shells process data in the form of

    strings. While this is a common way to represent data, it is not easy to extract and manipulate

    the data. Character sequences need to be found using regular expressions or line/column

    numbers and converted into the required string format for another command to process.

    In contrast, PowerShell is an object-based shell built on the .NET framework. Objects are a

    mechanism to store and manipulate data in a structured way. Data does not have to be

    extracted from strings and can be accessed using a simple naming convention.

    Note: PowerShell’s language is based on a POSIX standard shell (IEEE Spec.1003.2), which itself is based on the UNIX Bourne Shell (Windows PowerShell in

     Action – Second Edition, Manning Press 2011 by Bruce Payette).

    What is an Object?

    Objects are all around us. A car is an object with a collection of separate parts, such as asteering wheel, accelerator pedal and brakes. To drive the car, we can use the parts to steer,

    accelerate and slow/stop the vehicle. We can now divide the car (object) into two distinct

    concepts.

      A collection of parts

      Uses of the parts to change the car’s behavior  

     Now, apply this object model to the Windows Operating System.

    A Windows Service object has a collection of parts called Properties. Properties represent

    the state of a service, such as the service name and status. The service status can be changed

     by using object Methods. Object Methods allow you to start or stop a service. Collectively, properties and methods are called object Members.

    Members

    Properties Methods

    Service Name Start()

    Status Stop()

    Note: Method names can be easily distinguished from property names as they arealways appended with a pair of smooth brackets ‘()’. 

  • 8/15/2019 PowerShell for the IT Administrator Part 1 Lab Manual v1.1

    51/360

    Page 50 of 361

    Microsoft  | Services © 2012 Microsoft Corporation ITOE Educate 

    Microsoft Confidential

    Task 1: List Object Information (Get-Member Cmdlet)

    The .NET object framework is self-descriptive. All objects hold information that describes

    their structure. You can interrogate any .NET object and list its properties and methods within

    PowerShell without needing to refer to the online MSDN Class Library.

    You can achieve this by passing the object through the pipeline, to the Get-Member Cmdlet.Pipeline operations will be covered in detail in another lesson.

    1.  Get a list of process objects using the Get-Service Cmdlet and pipe them to the Get-

     Member  Cmdlet.

    This will list the members (properties & methods) of this type of object.

    Get-Service | Get-Member

    2.  Alternatively, you can choose not to use the pipeline and employ the Get-Member

    Cmdlet’s InputObject parameter. This command however lists the members of the

    collectin of pipeline data as a whole, rather than the individual items in the collection.

    Get-Member –InputObject (Get-Service)

    The top of the output lists the type name of the object(s). In the first case when piping to

    Ger-Member, you can see it is a System.ServiceProcess.ServiceController  type of object.

    TypeName: System.ServiceProcess.ServiceController

    The next piece of information displayed is the collection of members (properties and

    methods). The output below displays three columns of member information: the name,

    membertype and definition.

    Name MemberType Definition

    ---- ---------- ----------Name AliasProperty Name = ServiceName

    RequiredServices AliasProperty RequiredServices = ServicesDependedOn

    Disposed Event System.EventHandler Disposed(System.Object,

    Close Method System.Void Close()

    Continue Method System.Void Continue()

    CreateObjRef Method System.Runtime.Remoting.ObjRef

    Dispose Method System.Void Dispose()

    Equals Method bool Equ