PowerShell/Console
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]- Wikipedia: Command-line interface
- Wikipedia: Pipeline (Unix)
- Wikipedia: Redirection (computing)
- Wikipedia: Comment (computer programming)
- BonusBits: Mastering PowerShell Chapter 1 - The PowerShell Console
Multimedia
[edit | edit source]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]- Experiment with customizing the PowerShell console properties, including font, screen buffer size, and colors.
- 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.
- Use the Get-Host and Select-Object cmdlets and the pipeline to select the PowerShell version number.
- 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.
- 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).
- 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]Assessments
[edit | edit source]- Flashcards: Quizlet: Windows PowerShell - Console
- Quiz: Quizlet: Windows PowerShell - Console
See Also
[edit | edit source]- Microsoft TechNet Virtual Lab: Windows Server 2012 R2 - Windows PowerShell Fundamentals
- Microsoft Developer Network: Supporting Wildcard Characters in Cmdlet Parameters
References
[edit | edit source]- ↑ Wikipedia: Comparison of programming languages (syntax)
- ↑ Wikipedia: Comparison of programming languages (syntax)
- ↑ Microsoft TechNet: Get-Command
- ↑ Microsoft TechNet: Get-Date
- ↑ Microsoft TechNet: Get-Process
- ↑ Microsoft TechNet: Get-Service
- ↑ Microsoft TechNet: Select-Object
- ↑ Microsoft TechNet: Sort-Object
- ↑ Microsoft TechNet: Where-Object
- ↑ Microsoft TechNet: about_Redirection
- ↑ Wikipedia: Command-line interface
- ↑ Wikipedia: Command-line interface
- ↑ Wikipedia: Command-line interface
- ↑ Wikipedia: Command-line interface
- ↑ Wikipedia: Command-line interface
- ↑ Wikipedia: Command-line interface
- ↑ Wikipedia: Command-line interface
- ↑ Wikipedia: Command-line interface
- ↑ Wikipedia: Command-line interface
- ↑ Wikipedia: Command-line interface
- ↑ Wikipedia: Pipeline (Unix)
- ↑ Wikipedia: Pipeline (Unix)
- ↑ Wikipedia: Pipeline (Unix)
- ↑ Wikipedia: Redirection (computing)
- ↑ Wikipedia: Redirection (computing)
- ↑ Wikipedia: Redirection (computing)
- ↑ Wikipedia: Comment (computer programming)
- ↑ Wikipedia: Comparison of programming languages (syntax)
- ↑ Wikipedia: Comparison of programming languages (syntax)
- ↑ Master-PowerShell | With Dr. Tobias Weltner: Chapter 1. The PowerShell Console
- ↑ Master-PowerShell | With Dr. Tobias Weltner: Chapter 1. The PowerShell Console
- ↑ Master-PowerShell | With Dr. Tobias Weltner: Chapter 1. The PowerShell Console
- ↑ Master-PowerShell | With Dr. Tobias Weltner: Chapter 1. The PowerShell Console
- ↑ Master-PowerShell | With Dr. Tobias Weltner: Chapter 1. The PowerShell Console
- ↑ Microsoft TechNet: Get-Command
- ↑ Microsoft TechNet: Get-Date
- ↑ Microsoft TechNet: Get-Process
- ↑ Microsoft TechNet: Get-Service
- ↑ Microsoft TechNet: Select-Object
- ↑ Microsoft TechNet: Sort-Object
- ↑ Microsoft TechNet: Where-Object
- ↑ Wikipedia: Parameter (computer programming)
- ↑ Wikipedia: Data buffer
- ↑ Wikipedia: Default (computer science)
- ↑ Wikipedia: Delimiter
- ↑ Wikipedia: Environment variable
- ↑ Wikipedia: Escape character
- ↑ Wikipedia: Command-line option
- ↑ Wikipedia: Parameter (computer programming)
- ↑ Wikipedia: Pseudocode
- ↑ Wikipedia: Standard streams
- ↑ Wikipedia: Standard streams
- ↑ Wikipedia: Standard streams
- ↑ Wikipedia: Stream (computing)