PowerShell Basics: How to Integrate Cmdlets into Your Daily Sysadmin Tasks

Learn several day-to-day use cmdlets – an effective way to learn PowerShell.

Young Sys Admin working on a computerPowerShell is a key part of any Windows administrator’s toolbox. However, it can be difficult to know where to start or how to acquire a fundamental understanding of exactly what PowerShell is, what it does and why it’s so critical.

 PowerShell is a:

  • Shell, or command-line environment
  • Scripting language
  • Configuration management platform

At this point, Microsoft assumes PowerShell drives a great many sysadmin tasks. And PowerShell isn’t just for Windows anymore; versions are available on the Linux and macOS platforms, too. Let’s explore the fundamentals and suggest ways to develop PowerShell skills.

PowerShell includes robust features, such as:

  • Command history
  • Tab completion
  • Effective help system
  • Scriptability for repetitive tasks
  • Flexibility

PowerShell scripts can be developed by using the PowerShell Integrated Development Environment (IDE) and configuration management is automated by using PowerShell Desired State Configuration (DSC). A solid grasp of the basics is important before working with PowerShell in these more advanced ways.

PowerShell Syntax

PowerShell works by combining a verb and a noun into a component called a cmdlet. For example, the verb Get is combined with the noun Help to display the help feature. The resulting cmdlet is Get-Help.

The Get-Help cmdlet

Figure 1: The Get-Help cmdlet

Try the Get-Help cmdlet now on an available system.

Many cmdlets are this simple. Configurations can be displayed, basic values set or code executed. However, more information is usually needed. For example, a sysadmin wouldn’t just type the cmdlet New-ADUser. Lots of additional information, such as a username and other settings, is required.

PowerShell relies on parameters to define additional data the cmdlet needs to complete its task. The parameters vary by cmdlet. While cmdlets are not case sensitive, the values provided by parameters are.

Here’s an example that creates a new directory along a specified path:

New-Item -Path “C:\Projects\2022plans” -ItemType Directory

In this case, the parameters specify the item to be created and its type. Similarly, to delete the directory, the object must be identified.

Remove-Item “C:\Projects\2022plans”

Useful Cmdlets to Try

The examples we’ve looked at so far are certainly practical, but they are only meant to illustrate and identify some of the PowerShell components. The following examples detail several useful cmdlets. Challenge yourself to use these cmdlets regularly until they become a habit. Once they are, find a few more to integrate into your daily sysadmin tasks.

Display IP Address Information

One of the most common troubleshooting tasks for administrators is verifying IP address configurations. There are many ways PowerShell can help with this, but let’s begin with a simple cmdlet that displays critical settings. Here is the cmdlet:

Get-NetIPAddress -AddressFamily IPv4

The Get-NetIPAddress cmdlet

Figure 2: The Get-NetIPAddress cmdlet

Confirming the appropriate IP address is fundamental to network troubleshooting. Notice in this case the IPv4 address family was set. PowerShell can do even more, including upping and downing interfaces and setting static configurations. It can even replace the venerable ping with the Test-Connection cmdlet.

Display Services

Servers exist to provide services to users and client systems. Services like DHCP, file storage, printing and email are critical parts of the infrastructure. PowerShell easily displays the status of services and even allows for service management.

To display the status of the DHCP service, type:

Get-Service -Name DHCPServer

The Get-Service cmdlet

Figure 3: The Get-Service cmdlet

One basic way of handling apparent service failures is to restart the service. Instead of using the Services console, try opening the Windows PowerShell console instead, and then type:

Restart-Service -Name DHCPServer

The Restart-Service cmdlet

Figure 4: The Restart-Service cmdlet

In fact, DHCP can be managed entirely via PowerShell. Administrators can add the service, configure a scope, authorize the service in Active Directory, etc., all from PowerShell. Those who deploy DHCP regularly—in a lab or test environment—could script the entire service lifecycle.

Display Group Memberships

Another common help desk ticket involves users who are unable to access resources. Often, such issues are related to improper group memberships. Check Active Directory group membership for Administrators by using PowerShell with this cmdlet:

Get-ADGroupMember -Identity Administrators

The output displays group members, which then can be checked against NTFS and Share permissions to verify the appropriate settings are in place. Not only can group memberships be displayed, but groups can be created and removed with PowerShell and user accounts can be added or deleted, as well.

Confirm a File Exists

The last sample cmdlet checks whether a given file or directory exists. The Test-Path cmdlet checks for the presence of the indicated resource. For example, to find out whether the folder 2022plans exists in C:\Projects, type:

Test-Path -Path “C:\Projects\2022plans”

The Test-Path cmdlet

Figure 5: The Test-Path cmdlet

The resulting output will be either True or False. The Test-Path cmdlet could be written into a script that checks for the existence of the 2022plans directory and creates the directory if it does not already exist. Entire directory trees can be created and populated with files copied in from a central source.

Add PowerShell to Your Routine

Now you understand PowerShell’s verb-noun syntax and how parameters supplement the cmdlets. We’ve also covered PowerShell’s ability to provide scripting and configuration management solutions.

Scripting PowerShell cmdlets is where this language shines. PowerShell use can be anything from a simple cmdlet that displays basic information to an automation and configuration management tool that manages an entire group of servers.

To learn PowerShell, I suggest picking three to five PowerShell cmdlets related to your daily tasks and get in the habit of using them. Once they become second nature, select a few more cmdlets and start working them into your daily routine. Begin to combine cmdlets and even save these combinations into files for repeated use. Soon, you’ll be a PowerShell master, with a library of scripts and the ability to manage large environments and automation tasks easily.

Get more tutorials like this right in your inbox with CompTIA’s IT Career Newsletter. Subscribe today, and you can save 10% off your next CompTIA purchase.

Email us at [email protected] for inquiries related to contributed articles, link building and other web content needs.

Read More from the CompTIA Blog

Leave a Comment