5 PowerShell Cmdlets for Beginners

PowerShell: Essential Cmdlets for IT Professionals
PowerShell is rapidly gaining prominence as the scripting language and command-line interface (CLI) of choice for both experienced users and IT professionals. Acquiring proficiency in even a small set of commands can significantly enhance productivity.
Five Useful PowerShell Cmdlets
This article highlights five particularly useful cmdlets to help you begin your journey with PowerShell. These commands offer a foundation for automating tasks and managing systems effectively.
The following cmdlets are presented to provide a starting point for leveraging the power of PowerShell in your daily workflows.
- Get-Process: This cmdlet retrieves information about processes currently running on the system.
- Get-Service: Used to obtain details regarding services, including their status and configuration.
- Stop-Process: Allows you to terminate a running process by its ID or name.
- Restart-Service: This cmdlet facilitates the restarting of a specified service.
- Get-ChildItem: Retrieves a list of files and directories within a specified location.
Understanding how to utilize Get-Process allows for monitoring system resource usage. It's a crucial tool for identifying resource-intensive applications.
The Get-Service cmdlet is invaluable for managing system services. You can quickly check if a service is running or stopped.
When a process becomes unresponsive, Stop-Process provides a means to terminate it. Use caution when terminating processes, as it can lead to data loss.
Restart-Service is useful for applying configuration changes or resolving minor service issues. It offers a non-disruptive way to refresh a service.
Navigating the file system is simplified with Get-ChildItem. This cmdlet displays the contents of a directory, making file management more efficient.
Investing time in learning these PowerShell cmdlets will undoubtedly prove beneficial for any IT professional or power user. These tools empower you to automate tasks and streamline system administration.
The Get-Command Cmdlet
Get-Command stands out as a particularly valuable cmdlet within the PowerShell environment. It serves as a crucial tool for newcomers, facilitating exploration and understanding of PowerShell's capabilities through cmdlet searching.
Executing Get-Command without any parameters yields a comprehensive list of all available commands, which, while complete, isn't immediately practical for targeted searches.
Understanding Command Properties
However, the output reveals that PowerShell objects possess both a Name and a ModuleName property. This information enables refined searches based on specific criteria.
By leveraging these properties, users can pinpoint cmdlets containing desired terms.
Searching by Name
For instance, to locate all cmdlets related to "IP", the following command can be employed:
Get-Command –Name *IP*
This query returns a list of cmdlets whose names include the specified string.
Filtering by Module
Despite narrowing the search with a name pattern, the results may still be extensive. Further refinement can be achieved by specifying a particular module.
To illustrate, let's focus on the NetTCPIP module:
Get-Command –Module NetTCPIP –Name *IP*
This command restricts the search to cmdlets within the NetTCPIP module that also contain "IP" in their name, providing a more focused outcome.
Understanding and Utilizing Get-Help in PowerShell
After identifying the desired cmdlet with Get-Command, understanding its syntax and usage becomes crucial. This is where the Get-Help cmdlet proves invaluable.
Users familiar with the Windows command line may recall using the "/?" switch for help. However, PowerShell operates differently.
PowerShell's Parameter Handling
In PowerShell, spaces delineate commands from their parameters. Consequently, the traditional "/?" method is not applicable.
Instead, Get-Help is employed, with the cmdlet's name provided as a parameter.
For example, to access help information for the Get-Process cmdlet, the following command is used:
Get-Help Get-Process
This command will display detailed information about Get-Process, including its syntax, parameters, and examples.
The output from Get-Help provides a comprehensive resource for effectively utilizing PowerShell cmdlets.
By leveraging Get-Help, users can quickly learn and apply the functionalities of various cmdlets within the PowerShell environment.
Get-Member Cmdlet
The Get-Member cmdlet is utilized to retrieve information regarding the objects returned by other cmdlets. Its functionality is intrinsically linked to PowerShell's pipeline feature. To illustrate this, we will employ the Get-Process cmdlet as a practical example.
Understanding Object Properties
PowerShell's standard output typically displays a selection of object properties, conveniently presented as column headers. However, this initial view doesn't encompass the entirety of available properties.
Furthermore, the default output doesn't reveal the methods that can be invoked on the object. To gain a comprehensive understanding of both properties and methods, piping the output to Get-Member is essential.
Piping to Get-Member
The following command demonstrates how to pipe the output of Get-Process to Get-Member:
Get-Process | Get-Member
The resulting output provides a detailed listing of all available properties and methods associated with the process objects.
Practical Application
Although the output may initially seem complex, familiarity with Get-Member is crucial for effective PowerShell scripting. Its utility will become increasingly apparent as you gain experience.
Consider the following example:
Start-Process notepad.exe
$NotepadProc = Get-Process -Name notepad
$NotepadProc.WaitForExit()
Start-Process calc.exe
Script Explanation
This script initiates the launch of Notepad. Subsequently, the output of Get-Process -Name notepad is assigned to the variable $NotepadProc.
The WaitForExit() method is then called on $NotepadProc. This action pauses script execution until Notepad is closed by the user. Once Notepad is closed, the calculator application (calc.exe) is launched.
Therefore, Get-Member allows for detailed object inspection and manipulation, enabling more sophisticated scripting capabilities within PowerShell.
The Current Pipeline Object
Though technically not a cmdlet itself, the variable $_ is among the most frequently utilized special variables within PowerShell. Officially designated as “the current pipeline object,” its function is crucial in several contexts.
This variable finds application within script blocks, filters, and the process section of functions. It’s also essential when working with Where-Object, ForEach-Object, and switches.
Understanding its Use
The concept is best illustrated through an example, leading us to a discussion of the Where-Object cmdlet. This cmdlet demonstrates the power and utility of the current pipeline object.
Essentially, $_ represents the item currently being processed within the pipeline. Its availability simplifies operations by providing direct access to the object's properties and methods.
Consider scenarios where you need to filter or modify objects as they pass through the pipeline. Using $_ allows you to perform these actions concisely and efficiently.
Where Does it Apply?
- Script blocks
- Filters
- The process clause of functions
- Where-Object
- ForEach-Object
- Switches
These are the primary areas where you will encounter and leverage the functionality of the current pipeline object, denoted by $_.
Where-Object: Filtering Based on Criteria
The Where-Object cmdlet functions precisely as its name suggests: it filters and selects objects based on specified conditions. This process effectively combines the current object, represented by $_, with the properties accessible through Get-Member.
To illustrate this functionality, we will utilize Get-Process and pipe its output to the Where-Object cmdlet.
Get-Process | Where-Object {$_.Name –eq “iexplore”}
Understanding the Command
This command initiates by retrieving a list of all currently running processes on the system.
The pipe symbol (|) then forwards this process list as input to the Where-Object cmdlet.
Where-Object accepts a script block, enclosed in curly braces {}, which defines the filtering criteria.
The Script Block Explained
Within the script block, $_ represents the current process being evaluated.
The expression $_.Name –eq “iexplore” checks if the Name property of the current process is equal to “iexplore”.
Only processes where this condition is true are included in the final output.
Result and Application
Consequently, the command returns a list containing only the instances of Internet Explorer (iexplore) that are currently running.
This demonstrates a straightforward yet powerful method for isolating specific objects based on their properties within PowerShell. Experiment with different properties and criteria to refine your filtering capabilities.