Jump to content

PowerShell/Console

From Wikiversity

This lesson introduces the PowerShell console by looking at cmdlets, the pipeline, and redirection.

Objectives and Skills

[edit | edit source]

After completing this lesson, you will be able to:

  • Customize the PowerShell console interface.
  • Use the PowerShell console interface to run cmdlets.
  • Use the PowerShell pipeline to select, sort, and filter content.
  • Use redirection to save PowerShell output as a file.

Readings

[edit | edit source]
  1. Wikipedia: Command-line interface
  2. Wikipedia: Pipeline (Unix)
  3. Wikipedia: Redirection (computing)
  4. Wikipedia: Comment (computer programming)
  5. BonusBits: Mastering PowerShell Chapter 1 - The PowerShell Console

Multimedia

[edit | edit source]
  1. Microsoft Virtual Academy: Getting Started with PowerShell 3.0
  2. YouTube: Customizing the Shell

Examples

[edit | edit source]

Comments

[edit | edit source]

Comments are added to a PowerShell script using the hashtag or number sign character (#).[1]

# This is a PowerShell comment.

Block Comments

[edit | edit source]

Block comments are added to a PowerShell script using <# and #>.[2]

<#

This is a series or
block of comments.

#>

Get-Command

[edit | edit source]

The Get-Command cmdlet lists all PowerShell commands.[3]

Get-Command  # List all PowerShell commands.

Get-Date

[edit | edit source]

The Get-Date cmdlet returns the current date and time.[4]

Get-Date  # Get the current date and time.

Get-Process

[edit | edit source]

The Get-Process cmdlet lists all running processes.[5]

Get-Process  # List all running processes.

Get-Service

[edit | edit source]

The Get-Service cmdlet lists all installed services.[6]

Get-Service  # List all installed services.

Select-Object

[edit | edit source]

The Select-Object cmdlet selects objects or object properties.[7]

Get-Host | Select-Object 'Name'  # Displays the host's Name property only.

Sort-Object

[edit | edit source]

The Sort-Object cmdlet sorts objects based on property values.[8]

Get-Service | Sort-Object 'Status'  # Displays services sorted by Status.

Where-Object

[edit | edit source]

The Where-Object cmdlet selects objects based on their property values.[9]

Get-Process | Where-Object 'CPU' -GT 0  # Displays processes where the CPU utilization is greater than 0.

Redirection

[edit | edit source]

The > character is used to redirect pipeline output to a file. The >> combination appends pipeline output to the file.[10]

Get-Date > hostinfo.txt   # Create hostinfo.txt file with current date
Get-Host >> hostinfo.txt  # Append host information
Notepad hostinfo.txt    # Open hostinfo.txt in Notepad to view results

Activities

[edit | edit source]
  1. Experiment with customizing the PowerShell console properties, including font, screen buffer size, and colors.
  2. Review Microsoft TechNet: Using the Select-Object Cmdlet. Use PowerShell, the Get-Host and Select-Object cmdlets, and the pipeline to select the name of the host environment. Then use PowerShell ISE to test the same command.
  3. Use the Get-Host and Select-Object cmdlets and the pipeline to select the PowerShell version number.
  4. Review Microsoft TechNet: Using the Get-Command Cmdlet. Run the Get-Command cmdlet and redirect the output to a file. Open the file to view the results.
  5. Review Microsoft TechNet: Using the Get-Process Cmdlet and Microsoft TechNet: Using the Sort-Object Cmdlet. Use a combination of Get-Process and Sort-Object to display a list of the running processes sorted by CPU usage in descending order (greatest to least).
  6. Review Microsoft TechNet: Using the Get-Service Cmdlet and Microsoft TechNet: Using the Where-Object Cmdlet. Use the correct combination of Get-Service, Select-Object, Sort-Object, and Where-Object to display a list of running services sorted in alphabetical order by display name, showing the display name only.

Lesson Summary

[edit | edit source]
  • A command-line interface (CLI), also known as command-line user interface, console user interface, and character user interface (CUI), is a means of interacting with a computer program where the user (or client) issues commands to the program in the form of successive lines of text (command lines).[11]
  • The interface is usually implemented with a command line shell, which is a program that accepts commands as text input and converts commands to appropriate operating system functions.[12]
  • Programs with command-line interfaces are generally easier to automate via scripting.[13]
  • A command prompt (or just prompt) is a sequence of (one or more) characters used in a command-line interface to indicate readiness to accept commands.[14]
  • A CLI can generally be considered as consisting of syntax and semantics.[15]
  • The syntax is the grammar that all commands must follow. These rules also dictate how a user navigates through the system of commands.[16]
  • The semantics define what sort of operations are possible, on what sort of data these operations can be performed, and how the grammar represents these operations and data—the symbolic meaning in the syntax.[17]
  • A command-line argument or parameter is an item of information provided to a program when it is started.[18]
  • A command-line option or simply option (also known as a flag or switch) modifies the operation of a command.[19]
  • Although most users think of the shell as an interactive command interpreter, it is really a programming language in which each statement runs a command.[20]
  • A pipeline is a set of processes chained by their standard streams, so that the output of each process feeds directly as input to the next one.[21]
  • To use the pipeline, one writes the commands in sequence, separated by the ASCII vertical bar character "|" (which, for this reason, is often called "pipe character").[22]
  • By default, the standard error streams of the processes in a pipeline are not passed on through the pipe; instead, they are merged and directed to the console.[23]
  • Redirection is a function common to most command-line interpreters that can redirect standard streams to user-specified locations.[24]
  • Redirection is usually implemented by placing certain characters between commands. Typically, the syntax of these characters is as follows, using < to redirect input, and > to redirect output.[25]
  • To append output to the end of the file, rather than overwriting it, use the >> operator.[26]
  • A comment is a programming language construct used to embed programmer-readable annotations in the source code of a computer program.[27]
  • Comments are added to a PowerShell script using the hashtag or number sign character (#).[28]
  • Block comments are added to a PowerShell script using <# and #>.[29]
  • The name of the PowerShell console executable program is powershell.exe.[30]
  • PowerShell commands are cancelled by pressing <Ctrl>+<C>.[31]
  • The <Tab> key is used to complete PowerShell input automatically.[32]
  • The PowerShell console font, window size, buffer size, and other options are configured using the console Properties dialog box.[33]
  • PowerShell output may be displayed one page at a time by piping output through the More function, as in command | more.[34]
  • The Get-Command cmdlet lists all PowerShell commands.[35]
  • The Get-Date cmdlet returns the current date and time.[36]
  • The Get-Process cmdlet lists all running processes.[37]
  • The Get-Service cmdlet lists all installed services.[38]
  • The Select-Object cmdlet selects objects or object properties.[39]
  • The Sort-Object cmdlet sorts objects based on property values.[40]
  • The Where-Object cmdlet selects objects based on their property values.[41]

Key Terms

[edit | edit source]
argument
One of the pieces of data provided as input in a parameter to a subroutine.[42]
buffer
A region of a physical memory storage used to temporarily store data while it is being moved from one place to another.[43]
default
A setting or a value automatically assigned to a software application, computer program or device, outside of user intervention.[44]
delimiter
A sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data streams.[45]
environment variable
A dynamic named value that can affect the way running processes will behave on a computer.[46]
escape character
A character which invokes an alternative interpretation on subsequent characters in a character sequence.[47]
option
Text that modifies the operation of a command, with the effect determined by the command's program.[48]
parameter
A special kind of variable, used in a subroutine to refer to one of the pieces of data, or arguments, provided as input to a subroutine.[49]
pseudocode
An informal high-level description of the operating principle of a computer program or other algorithm.[50]
stdin (standard input)
The stream of data (often text) going into a program.[51]
stdout (standard output)
The stream where a program writes its output data.[52]
stderr (standard error)
Another output stream typically used by programs to output error messages or diagnostics.[53]
stream
A sequence of data elements made available over time.[54]

Review Questions

[edit | edit source]
Enable JavaScript to hide answers.
Click on a question to see the answer.
1. A command-line interface (CLI), also known as command-line user interface, console user interface, and character user interface (CUI), is a means of interacting with a computer program where the user (or client) _____.
A command-line interface (CLI), also known as command-line user interface, console user interface, and character user interface (CUI), is a means of interacting with a computer program where the user (or client) issues commands to the program in the form of successive lines of text (command lines).
2. The interface is usually implemented with a command line shell, which is _____.
The interface is usually implemented with a command line shell, which is a program that accepts commands as text input and converts commands to appropriate operating system functions.
3. Programs with command-line interfaces are generally easier to _____.
Programs with command-line interfaces are generally easier to automate via scripting.
4. A command prompt (or just prompt) is a _____.
A command prompt (or just prompt) is a sequence of (one or more) characters used in a command-line interface to indicate readiness to accept commands.
5. A CLI can generally be considered as consisting of _____.
A CLI can generally be considered as consisting of syntax and semantics.
6. The syntax is _____.
The syntax is the grammar that all commands must follow. These rules also dictate how a user navigates through the system of commands.
7. The semantics define _____.
The semantics define what sort of operations are possible, on what sort of data these operations can be performed, and how the grammar represents these operations and data—the symbolic meaning in the syntax.
8. A command-line argument or parameter is _____.
A command-line argument or parameter is an item of information provided to a program when it is started.
9. A command-line option or simply option (also known as a flag or switch) _____.
A command-line option or simply option (also known as a flag or switch) modifies the operation of a command.
10. Although most users think of the shell as an interactive command interpreter, it is really _____.
Although most users think of the shell as an interactive command interpreter, it is really a programming language in which each statement runs a command.
11. A pipeline is _____.
A pipeline is a set of processes chained by their standard streams, so that the output of each process feeds directly as input to the next one.
12. To use the pipeline, one _____.
To use the pipeline, one writes the commands in sequence, separated by the ASCII vertical bar character "
13. By default, the standard error streams of the processes in a pipeline _____.
By default, the standard error streams of the processes in a pipeline are not passed on through the pipe; instead, they are merged and directed to the console.
14. Redirection is a function common to most command-line interpreters that can _____.
Redirection is a function common to most command-line interpreters that can redirect standard streams to user-specified locations.
15. Redirection is usually implemented by placing certain characters between commands. Typically, the syntax of these characters is as follows, using _____ to redirect input, and _____ to redirect output.
Redirection is usually implemented by placing certain characters between commands. Typically, the syntax of these characters is as follows, using < to redirect input, and > to redirect output.
16. To append output to the end of the file, rather than overwriting it, use the _____ operator.
To append output to the end of the file, rather than overwriting it, use the >> operator.
17. A comment is a programming language construct used to _____.
A comment is a programming language construct used to embed programmer-readable annotations in the source code of a computer program.
18. Comments are added to a PowerShell script using _____.
Comments are added to a PowerShell script using the hashtag or number sign character (#).
19. Block comments are added to a PowerShell script using _____.
Block comments are added to a PowerShell script using <# and #>.
20. The name of the PowerShell console executable program is _____.
The name of the PowerShell console executable program is powershell.exe.
21. PowerShell commands are cancelled by pressing _____.
PowerShell commands are cancelled by pressing <Ctrl>+<C>.
22. The <Tab> key is used to _____.
The <Tab> key is used to complete PowerShell input automatically.
23. The PowerShell console font, window size, buffer size, and other options are configured using _____.
The PowerShell console font, window size, buffer size, and other options are configured using the console Properties dialog box.
24. PowerShell output may be displayed one page at a time by _____.
PowerShell output may be displayed one page at a time by piping output through the More function, as in command
25. The Get-Command cmdlet _____.
The Get-Command cmdlet lists all PowerShell commands.
26. The Get-Date cmdlet _____.
The Get-Date cmdlet returns the current date and time.
27. The Get-Process cmdlet _____.
The Get-Process cmdlet lists all running processes.
28. The Get-Service cmdlet _____.
The Get-Service cmdlet lists all installed services.
29. The Select-Object cmdlet _____.
The Select-Object cmdlet selects objects or object properties.
30. The Sort-Object cmdlet _____.
The Sort-Object cmdlet sorts objects based on property values.
31. The Where-Object cmdlet _____.
The Where-Object cmdlet selects objects based on their property values.

Assessments

[edit | edit source]

See Also

[edit | edit source]

References

[edit | edit source]
Type classification: this is a lesson resource.
Completion status: this resource is considered to be complete.
  1. Wikipedia: Comparison of programming languages (syntax)
  2. Wikipedia: Comparison of programming languages (syntax)
  3. Microsoft TechNet: Get-Command
  4. Microsoft TechNet: Get-Date
  5. Microsoft TechNet: Get-Process
  6. Microsoft TechNet: Get-Service
  7. Microsoft TechNet: Select-Object
  8. Microsoft TechNet: Sort-Object
  9. Microsoft TechNet: Where-Object
  10. Microsoft TechNet: about_Redirection
  11. Wikipedia: Command-line interface
  12. Wikipedia: Command-line interface
  13. Wikipedia: Command-line interface
  14. Wikipedia: Command-line interface
  15. Wikipedia: Command-line interface
  16. Wikipedia: Command-line interface
  17. Wikipedia: Command-line interface
  18. Wikipedia: Command-line interface
  19. Wikipedia: Command-line interface
  20. Wikipedia: Command-line interface
  21. Wikipedia: Pipeline (Unix)
  22. Wikipedia: Pipeline (Unix)
  23. Wikipedia: Pipeline (Unix)
  24. Wikipedia: Redirection (computing)
  25. Wikipedia: Redirection (computing)
  26. Wikipedia: Redirection (computing)
  27. Wikipedia: Comment (computer programming)
  28. Wikipedia: Comparison of programming languages (syntax)
  29. Wikipedia: Comparison of programming languages (syntax)
  30. Master-PowerShell | With Dr. Tobias Weltner: Chapter 1. The PowerShell Console
  31. Master-PowerShell | With Dr. Tobias Weltner: Chapter 1. The PowerShell Console
  32. Master-PowerShell | With Dr. Tobias Weltner: Chapter 1. The PowerShell Console
  33. Master-PowerShell | With Dr. Tobias Weltner: Chapter 1. The PowerShell Console
  34. Master-PowerShell | With Dr. Tobias Weltner: Chapter 1. The PowerShell Console
  35. Microsoft TechNet: Get-Command
  36. Microsoft TechNet: Get-Date
  37. Microsoft TechNet: Get-Process
  38. Microsoft TechNet: Get-Service
  39. Microsoft TechNet: Select-Object
  40. Microsoft TechNet: Sort-Object
  41. Microsoft TechNet: Where-Object
  42. Wikipedia: Parameter (computer programming)
  43. Wikipedia: Data buffer
  44. Wikipedia: Default (computer science)
  45. Wikipedia: Delimiter
  46. Wikipedia: Environment variable
  47. Wikipedia: Escape character
  48. Wikipedia: Command-line option
  49. Wikipedia: Parameter (computer programming)
  50. Wikipedia: Pseudocode
  51. Wikipedia: Standard streams
  52. Wikipedia: Standard streams
  53. Wikipedia: Standard streams
  54. Wikipedia: Stream (computing)