Getting to Know the Three Key Pillars of PowerShell for Windows Administration – Cmdlets, Objects, and Pipelines

How to Combine Cmdlets for More Complicated Solutions

Pipelines make it easy to combine cmdlets and create more complex solutions without the need to write difficult code. For example, if you want to find a process that is using a high CPU and stop it, you can use the following pipeline:

Get-Process |
Where-Object {$_.CPU -gt 50} |
Stop-Process
  • Where-Object {$_.CPU -gt 50}: Filters processes that have more than 50% CPU usage.
  • Stop-Process: Stops the filtered process.

This pipeline allows you to automate tasks with clear and organized steps. You can also add parameters to make the solution more specific, such as sending a report of the results:

Get-Process |
Where-Object {$_.CPU -gt 50} |
Export-Csv -Path “HighCPUProcesses.csv”

This pipeline will store the data of the CPU-intensive process in a CSV file.

Automation with PowerShell

One very useful cmdlet in PowerShell is Get-WindowsOptionalFeature. This cmdlet allows users to view optional features available in Windows, including their status (whether enabled or disabled). Using these cmdlets, administrators can easily manage and audit existing features.

To display all the optional features in Windows, you can run the following command:

Get-WindowsOptionalFeature -Online

This command will generate a list of objects that represent each optional feature, complete with properties such as FeatureName, State, and a description of the feature.

Steps to Create a Simple Script for Automation

Here are the steps to create a simple script that uses Get-WindowsOptionalFeature to automatically audit and enable certain features:

  1. Run PowerShell with administrator privileges to make sure you have the necessary permissions.
  2. Use the Get-WindowsOptionalFeature cmdlet to get a list of all optional features.
$features = Get-WindowsOptionalFeature -Online
  1. For example, if you want to check whether the “TelnetClient” feature is enabled or not, you can filter the results.
$telnetFeature = $features | Where-Object { $_. FeatureName -eq 'TelnetClient' }
  1. If the feature is not active, you can enable it by using the Enable-WindowsOptionalFeature cmdlet.
if ($telnetFeature.State  -eq 'Disabled') {
Enable-WindowsOptionalFeature -Online -FeatureName 'TelnetClient' -All
Write-Host “TelnetClient feature has been enabled.”
} else {
Write-Host “TelnetClient feature is now active.”
}
  1. Save this script as a .ps1 file, for example, EnableTelnet.ps1, and run it in PowerShell.

Latest Articles