This lesson introduces PowerShell ISE, the integrated scripting environment by examining the scripting environment, automatic code completion, and by creating simple scripts.

Objectives and Skills

edit

After completing this lesson, you will be able to:

  • Modify execution policy settings to allow locally written scripts to run.
  • Understand cmdlet aliases.
  • Explain the difference between Write-Host and Write-Output.
  • Use Write-Output to display messages to the user.
  • Stop and start services using a PowerShell script.

Readings

edit
  1. Wikipedia: Integrated development environment
  2. Wikipedia: Intelligent code completion
  3. Microsoft TechNet: Using the Windows PowerShell ISE
  4. BonusBits: Mastering PowerShell Chapter 2 - Interactive PowerShell

Multimedia

edit
  1. YouTube: Microsoft Windows 8 First Look: Windows PowerShell
  2. Microsoft Virtual Academy: Advanced Tools & Scripting with PowerShell 3.0

Examples

edit

Get-Alias

edit

The Get-Alias cmdlet displays a list of current Windows PowerShell aliases.[1]

Get-Alias    # List current aliases.

Get-ExecutionPolicy

edit

The Get-ExecutionPolicy cmdlet returns the current Windows PowerShell execution policy security level.[2]

Get-ExecutionPolicy    # Show the current PowerShell execution policy security level.

Set-ExecutionPolicy

edit

The Set-ExecutionPolicy cmdlet sets the Windows PowerShell execution policy security level.[3]

Set-ExecutionPolicy RemoteSigned    # Set the current execution policy security level to RemoteSigned.

Start-Service

edit

The Start-Service cmdlet starts a stopped service or services.[4] Starting services on Windows requires running PowerShell or the corresponding script as an administrator.

Start-Service 'Spooler'    # Start the Print Spooler service.

Stop-Service

edit

The Stop-Service cmdlet stops a running service or services.[5] Stopping services on Windows requires running PowerShell or the corresponding script as an administrator.

Stop-Service 'Spooler'    # Stop the Print Spooler service.

Write-Host

edit

The Write-Host cmdlet writes directly to the host environment, bypassing the pipeline.[6]

Write-Host 'Hello PowerShell!'

Write-Output

edit

The Write-Output cmdlet writes to the pipeline.[7]

Write-Output 'Hello PowerShell!'

Comparing Write-Host and Write-Output

edit

When there are no other commands in the pipeline, Write-Host and Write-Output appear functionally identical. The difference is clear, however, when the pipeline is used. To provide the most functionality for future use and automation of PowerShell scripts, Write-Output is the preferred output cmdlet.[8] The Get-Date cmdlet may be used to demonstrate the difference between Write-Host and Write-Output.

# This script demonstrates the difference between Write-Host and Write-Output

Write-Host '1/1/01' | Get-Date      # Displays 1/1/01 (no pipeline content).
Write-Output '1/1/01' | Get-Date    # Displays the formatted date.
'1/1/01' | Get-Date                 # Displays the formatted date.

Activities

edit
  1. Review Microsoft TechNet: Using the Set-ExecutionPolicy Cmdlet. Change your local execution policy to RemoteSigned so that you can run your own local saved scripts.
  2. Review Microsoft TechNet: Using the Get-Alias Cmdlet. Display a list of all Windows PowerShell aliases.
  3. Review Microsoft TechNet: Using the Write-Host Cmdlet, Microsoft TechNet: Write-Output, and Microsoft TechNet: Using the Get-Date Cmdlet. Experiment with the different commands and the pipeline to ensure you understand the difference between Write-Host and Write-Output.
  4. Review Microsoft TechNet: Write-Output. Create a script that uses Write-Output to display your name. Try something like 'Hello Wikiversity!'. Add a comment at the top of the script that describes the purpose of the script. Then save the script as a file and experiment with running the script file using both PowerShell and PowerShell ISE.
  5. Review Microsoft TechNet: Using the Stop-Service Cmdlet and Microsoft TechNet: Using the Start-Service Cmdlet. Use Get-Service to get a list of running services. Then write a script that will stop and restart the Print Spooler service. Add a comment at the top of the script that describes the purpose of the script. Save the script as a file and experiment with running the script file using both PowerShell and PowerShell ISE.

Lesson Summary

edit
  • An integrated development environment (IDE) or interactive development environment is a software application that provides comprehensive facilities to computer programmers for software development.[9]
  • An IDE normally consists of a source code editor, build automation tools and a debugger.[10]
  • Most modern IDEs offer Intelligent code completion features.[11]
  • Automatic code completion in PowerShell and PowerShell ISE is accomplished using the <Tab> key.[12]
  • Cmdlet parameters are automatically listed by entering the dash or hyphen (-) character and then using the <Tab> key to cycle through the list.[13]
  • New PowerShell tabs are created using the File menu.[14]
  • Remote PowerShell tabs may be created to establish a session on a remote computer.[15]
  • The PowerShell ISE console executes commands when you press <Enter>.[16]
  • Multiple commands may be executed together in the PowerShell ISE console in sequence by separating them using <Shift>+<Enter>.[17]
  • To stop a command in PowerShell ISE, on the toolbar, click Stop Operation, or press <Ctrl>+<Break>.[18]
  • The default Windows PowerShell execution policy setting is Restricted.[19]
  • PowerShell ISE script breakpoints can be set using Toggle Breakpoint or by pressing the <F9> key.[20]
  • PowerShell profiles may be established by configuring a PowerShell script to run automatically when you start a new PowerShell or PowerShell ISE session.[21]
  • Saved PowerShell scripts are run using a full or relative path. The relative path for a PowerShell script in the current directory would be .\script.ps1.[22]
  • The Get-Alias cmdlet displays a list of current Windows PowerShell aliases.[23]
  • The Get-ExecutionPolicy cmdlet returns the current Windows PowerShell execution policy security level.[24]
  • The Set-ExecutionPolicy cmdlet sets the Windows PowerShell execution policy security level.[25]
  • The Start-Service cmdlet starts a stopped service or services.[26]
  • The Stop-Service cmdlet stops a running service or services.[27]
  • Starting and stopping services on Windows requires running PowerShell or the corresponding script as an administrator.
  • The Write-Host cmdlet writes directly to the host environment, bypassing the pipeline.[28]
  • The Write-Output cmdlet writes to the pipeline.[29]

Key Terms

edit
breakpoint
An intentional stopping or pausing place in a program, put in place for debugging purposes.[30]
debugging
A methodical process of finding and reducing the number of bugs, or defects, in a computer program or a piece of electronic hardware, thus making it behave as expected.[31]

Review Questions

edit
Enable JavaScript to hide answers.
Click on a question to see the answer.
1. An integrated development environment (IDE) or interactive development environment is a _____.
An integrated development environment (IDE) or interactive development environment is a software application that provides comprehensive facilities to computer programmers for software development.
2. An IDE normally consists of _____.
An IDE normally consists of a source code editor, build automation tools and a debugger.
3. Most modern IDEs offer _____.
Most modern IDEs offer Intelligent code completion features.
4. Automatic code completion in PowerShell and PowerShell ISE is accomplished using the _____ key.
Automatic code completion in PowerShell and PowerShell ISE is accomplished using the <Tab> key.
5. Cmdlet parameters are automatically listed by entering the _____ character and then using the _____ key to cycle through the list.
Cmdlet parameters are automatically listed by entering the dash or hyphen (-) character and then using the <Tab> key to cycle through the list.
6. New PowerShell tabs are created using the _____ menu.
New PowerShell tabs are created using the File menu.
7. Remote PowerShell tabs may be created to _____.
Remote PowerShell tabs may be created to establish a session on a remote computer.
8. The PowerShell ISE console executes commands when you press _____.
The PowerShell ISE console executes commands when you press <Enter>.
9. Multiple commands may be executed together in the PowerShell ISE console in sequence by separating them using _____.
Multiple commands may be executed together in the PowerShell ISE console in sequence by separating them using <Shift>+<Enter>.
10. To stop a command in PowerShell ISE, on the toolbar, click _____, or press _____.
To stop a command in PowerShell ISE, on the toolbar, click Stop Operation, or press <Ctrl>+<Break>.
11. The default Windows PowerShell execution policy setting is _____.
The default Windows PowerShell execution policy setting is Restricted.
12. PowerShell ISE script breakpoints can be set using _____ or by pressing the _____ key.
PowerShell ISE script breakpoints can be set using Toggle Breakpoint or by pressing the <F9> key.
13. PowerShell profiles may be established by _____.
PowerShell profiles may be established by configuring a PowerShell script to run automatically when you start a new PowerShell or PowerShell ISE session.
14. Saved PowerShell scripts are run using _____.
Saved PowerShell scripts are run using a full or relative path. The relative path for a PowerShell script in the current directory would be .\script.ps1.
15. The Get-Alias cmdlet _____.
The Get-Alias cmdlet displays a list of current Windows PowerShell aliases.
16. The Get-ExecutionPolicy cmdlet _____.
The Get-ExecutionPolicy cmdlet returns the current Windows PowerShell execution policy security level.
17. The Set-ExecutionPolicy cmdlet _____.
The Set-ExecutionPolicy cmdlet sets the Windows PowerShell execution policy security level.
18. The Start-Service cmdlet _____.
The Start-Service cmdlet starts a stopped service or services.
19. The Stop-Service cmdlet _____.
The Stop-Service cmdlet stops a running service or services.
20. Starting and stopping services on Windows requires _____.
Starting and stopping services on Windows requires running PowerShell or the corresponding script as an administrator.
21. The Write-Host cmdlet _____.
The Write-Host cmdlet writes directly to the host environment, bypassing the pipeline.
22. The Write-Output cmdlet _____.
The Write-Output cmdlet writes to the pipeline.

Assessments

edit

See Also

edit

References

edit
  Type classification: this is a lesson resource.
  Completion status: this resource is considered to be complete.
  1. Microsoft TechNet: Get-Alias
  2. Microsoft TechNet: Get-ExecutionPolicy
  3. Microsoft TechNet: Set-ExecutionPolicy
  4. Microsoft TechNet: Start-Service
  5. Microsoft TechNet: Stop-Service
  6. Microsoft TechNet: Write-Host
  7. Microsoft TechNet: Write-Output
  8. Jeffrey Snover's blog: Write-Host Considered Harmful
  9. Wikipedia: Integrated development environment
  10. Wikipedia: Integrated development environment
  11. Wikipedia: Integrated development environment
  12. Microsoft TechNet: How to Use Tab Completion
  13. Microsoft TechNet: How to Use Tab Completion
  14. Microsoft TechNet: How to Create a PowerShell Tab in Windows PowerShell ISE
  15. Microsoft TechNet: How to Create a PowerShell Tab in Windows PowerShell ISE
  16. Microsoft TechNet: How to Use the Console Pane in the Windows PowerShell ISE
  17. Microsoft TechNet: How to Use the Console Pane in the Windows PowerShell ISE
  18. Microsoft TechNet: How to Use the Console Pane in the Windows PowerShell ISE
  19. Microsoft TechNet: How to Write and Run Scripts in the Windows PowerShell ISE
  20. Microsoft TechNet: How to Debug Scripts in Windows PowerShell ISE
  21. Microsoft TechNet: How to Use Profiles in Windows PowerShell ISE
  22. PowerShell.com: Master-PowerShell | With Dr. Tobias Weltner - Chapter 2. Interactive PowerShell
  23. Microsoft TechNet: Get-Alias
  24. Microsoft TechNet: Get-ExecutionPolicy
  25. Microsoft TechNet: Set-ExecutionPolicy
  26. Microsoft TechNet: Start-Service
  27. Microsoft TechNet: Stop-Service
  28. Microsoft TechNet: Write-Host
  29. Microsoft TechNet: Write-Output
  30. Wikipedia: Breakpoint
  31. Wikipedia: Debugging