Powershell

PowerShell is a versatile tool that works across different platforms. It combines:

  • A command-line interface (CLI)

  • A programming language for scripting

  • Tools for managing system settings

Its main purpose is to help automate repetitive tasks, like handling large sets of data, and to simplify the creation and execution of scripts that manage system operations. This makes it an essential utility for automating system maintenance and managing configurations efficiently.

Getting Started with Powershell

PowerShell cmdlets consist of a verb and a noun

Many native windows commands will work in the PowerShell window. For example ping, Ipconfig

  1. Open Powershell and run it as an administrator to remove any restrictions on the commands you can execute.

  1. Let's try a few basic commands first. There are many aliases to make transitioning to the PowerShell environment easier.

  • cls –> Clear-Host

  • cd –> Set-Location dir

  • ls –> Get-Children

  1. PowerShell's help system is robust, thoughtfully designed, and serves as an excellent reference tool. It also receives frequent updates to correct errors and enhance usability.

Use

Update-Help -Force 

cmdlet in PowerShell while connected to the Internet to update the PowerShell help files.

  1. After updating the Help function in PowerShell, use the 'Get-Help' cmdlet to retrieve information about a few cmdlets containing a specific keyword.

    For example:

  • Get-Help *Process* in PowerShell retrieves help information about any cmdlets, functions, or modules containing "Process" in their names.

  • Get-Help *Service* in PowerShell retrieves help information about any cmdlets, functions, or modules containing "Service" in their names.

The asterisks (*) act as wildcards, representing zero or more characters

  1. The Pipeline - The piping character sends the output from one cmdlet to the input for another cmdlet. It can all be typed on 1 line or it can be broken up onto multiple lines as shown in the two images.

  1. Displaying information in GUI - Grid-view windows makes this possible

    For example

 Get-Service | Out-Gridview
  1. Cmdlets that kill - Most commonly used cmdlets to terminate an application or process through PowerShell are:

  • Stop-Process

  • Kill-Process

When uncertain about the outcome of a command, you can use the -WhatIf parameter to simulate its execution without making any changes. Alternatively, the -Confirm parameter prompts you for confirmation before proceeding with an action.

For example:

Stop-Process -Name "chrome" -whatif
Stop-Process -Name "chrome" -Confirm
  1. Finding and Adding Modules - Get-Module will list installed modules, using the –ListAvailable parameter shows the available PowerShell modules and Import-Module will add the module that you need.

  1. Selecting Obejcts - The output of a cmdlet normally has a default set of properties that will be displayed, this can be modified to add or remove displayed properties by using the Select-Object cmdlet.

  1. Sorting Objects - Pipping the output of a cmdlet to the Sort-Object cmdlet allows sorting of the output in a descending or an ascending manner.

Powershell in Windows Server

  1. Let's install DHCP, DNS and Active Directory services through PowerShell

Install-WindowsFeature -Name DHCP -IncludeManagementTools
Install-WindowsFeature -Name DNS -IncludeManagementTools
Install-WindowsFeature -Name AD-Domain-Services -IncludeManagementTools

I already have ADDS install on my Domain Controller so the Exit Code is NoChangeNeeded and nothing was installed

  1. Let's explore a few more Powershell commands on the Windows Server Domain Controller.

Last updated