PowerShell/ISE
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 | edit source]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 | edit source]- Wikipedia: Integrated development environment
- Wikipedia: Intelligent code completion
- Microsoft TechNet: Using the Windows PowerShell ISE
- BonusBits: Mastering PowerShell Chapter 2 - Interactive PowerShell
Multimedia
[edit | edit source]- YouTube: Microsoft Windows 8 First Look: Windows PowerShell
- Microsoft Virtual Academy: Advanced Tools & Scripting with PowerShell 3.0
Examples
[edit | edit source]Get-Alias
[edit | edit source]The Get-Alias cmdlet displays a list of current Windows PowerShell aliases.[1]
Get-Alias # List current aliases.
Get-ExecutionPolicy
[edit | edit source]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 | edit source]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 | edit source]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 | edit source]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 | edit source]The Write-Host cmdlet writes directly to the host environment, bypassing the pipeline.[6]
Write-Host 'Hello PowerShell!'
Write-Output
[edit | edit source]The Write-Output cmdlet writes to the pipeline.[7]
Write-Output 'Hello PowerShell!'
Comparing Write-Host and Write-Output
[edit | edit source]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 | edit source]- 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.
- Review Microsoft TechNet: Using the Get-Alias Cmdlet. Display a list of all Windows PowerShell aliases.
- 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.
- 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.
- 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 | edit source]- 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 | edit source]- 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 | edit source]Assessments
[edit | edit source]- Flashcards: Quizlet: Windows PowerShell - ISE
- Quiz: Quizlet: Windows PowerShell - ISE
See Also
[edit | edit source]- Microsoft TechNet Virtual Lab: Windows Server 2012 R2 - Windows PowerShell Fundamentals
- PowerGUI: Free PowerShell IDE
References
[edit | edit source]- ↑ Microsoft TechNet: Get-Alias
- ↑ Microsoft TechNet: Get-ExecutionPolicy
- ↑ Microsoft TechNet: Set-ExecutionPolicy
- ↑ Microsoft TechNet: Start-Service
- ↑ Microsoft TechNet: Stop-Service
- ↑ Microsoft TechNet: Write-Host
- ↑ Microsoft TechNet: Write-Output
- ↑ Jeffrey Snover's blog: Write-Host Considered Harmful
- ↑ Wikipedia: Integrated development environment
- ↑ Wikipedia: Integrated development environment
- ↑ Wikipedia: Integrated development environment
- ↑ Microsoft TechNet: How to Use Tab Completion
- ↑ Microsoft TechNet: How to Use Tab Completion
- ↑ Microsoft TechNet: How to Create a PowerShell Tab in Windows PowerShell ISE
- ↑ Microsoft TechNet: How to Create a PowerShell Tab in Windows PowerShell ISE
- ↑ Microsoft TechNet: How to Use the Console Pane in the Windows PowerShell ISE
- ↑ Microsoft TechNet: How to Use the Console Pane in the Windows PowerShell ISE
- ↑ Microsoft TechNet: How to Use the Console Pane in the Windows PowerShell ISE
- ↑ Microsoft TechNet: How to Write and Run Scripts in the Windows PowerShell ISE
- ↑ Microsoft TechNet: How to Debug Scripts in Windows PowerShell ISE
- ↑ Microsoft TechNet: How to Use Profiles in Windows PowerShell ISE
- ↑ PowerShell.com: Master-PowerShell | With Dr. Tobias Weltner - Chapter 2. Interactive PowerShell
- ↑ Microsoft TechNet: Get-Alias
- ↑ Microsoft TechNet: Get-ExecutionPolicy
- ↑ Microsoft TechNet: Set-ExecutionPolicy
- ↑ Microsoft TechNet: Start-Service
- ↑ Microsoft TechNet: Stop-Service
- ↑ Microsoft TechNet: Write-Host
- ↑ Microsoft TechNet: Write-Output
- ↑ Wikipedia: Breakpoint
- ↑ Wikipedia: Debugging