PowerShell Part 1

PowerShell is a great tool for managing windows and if you work with Microsoft products it’s definitely worth learning.  I’ll be going over some of the basics below but one of the most useful things to know is once you have started typing a command you can press the tab key to autocomplete, if it doesn’t automatically pick the correct commend you can continue pressing tab until the correct one is shown.  This also works for additional parameters and paths but I’ll go in to more detail about these later.

Version of PowerShell

As of writing v5 is the latest release of PowerShell and what we’ll be working with here, to check which version you have you can enter $host or $PSVersionTable at the prompt and press return to run the commands.

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\David>$host


Name             : ConsoleHost
Version          : 5.1.16299.251
InstanceId       : 488c62a1-d7d4-4268-93f5-465af8ae4979
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-GB
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace



PS C:\Users\David> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.16299.251
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.16299.251
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

From the outputs we can see that both Version and PSVersion show that I’m running major release 5, hopefully yours will show similar.

The help system

PowerShell has a fantastic help system that explains all the inbuilt commands with lengthy descriptions and examples available to ensure you get the most from the system, first you should ensure you have the latest help files.

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\WINDOWS\system32> update-help

We’ll use the Get-Service command to test the help system, you can either use Get-Help Get-Service or Get-Service -? although the former will allow you to append further options ie. -examples to see examples of the command in use, -detailed for even more information or -full for technical information or -online to see the online version.

PS C:\WINDOWS\system32> get-help Get-Service

NAME
    Get-Service

SYNOPSIS
    Gets the services on a local or remote computer.


SYNTAX
    Get-Service [-ComputerName <String[]>] [-DependentServices] -DisplayName <String[]> [-Exclude <String[]>]
    [-Include <String[]>] [-RequiredServices] [<CommonParameters>]
PS C:\WINDOWS\system32> Get-Service -?

NAME
    Get-Service

SYNOPSIS
    Gets the services on a local or remote computer.


SYNTAX
    Get-Service [-ComputerName <String[]>] [-DependentServices] -DisplayName <String[]> [-Exclude <String[]>]
    [-Include <String[]>] [-RequiredServices] [<CommonParameters>]

Two more very useful commands are Get-Command which lists all cmdlets, aliases, functions, workflows, filters, scripts and applications available for use within PowerShell and Get-Alias which will show you aliases that can be used in place of the full commands.  If you are ever looking up scripts online they will sometimes be written using these aliases and you will be pleased to know that you can use these with help the same as the full command to get further details or search for them with Get-Alias ie. Get-Alias gsv.

PS C:\WINDOWS\system32> get-help gsv

NAME
    Get-Service

SYNOPSIS
    Gets the services on a local or remote computer.


SYNTAX
    Get-Service [-ComputerName <String[]>] [-DependentServices] -DisplayName <String[]> [-Exclude <String[]>]
    [-Include <String[]>] [-RequiredServices] [<CommonParameters>]
PS C:\WINDOWS\system32> Get-Alias gsv

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           gsv -> Get-Service

You can also use the Help command which gives nearly the same output as Get-Help but it automatically pauses after a page of information and you can either press return to move down one line or space to scroll down the next page.  This is similar to adding | more to the end of the statement.  Help also accepts the additional parameters -full, -detailed, -examples etc.

If you would like the help system to pop out in a seperate window you can append -ShowWindow to the command.

Syntax

Nearly all PowerShell commands are written as a verb which gives an indication of what the command will do, a dash followed by a noun related to the product.

Nearly all parameters require their parameter name to be used before any data unless unless it is within two sets of square brackets like [[-Name] <String[]>] below, this means that when running Get-Service you can just put the service name as long as it is the first thing to proceed the command.  To double check this you can run help -Full and double check that the Parameter has a position value similar to below instead of named.

Get-Service [[-Name] <String[]>] [-ComputerName <String[]>] [-DependentServices] [-Exclude <String[]>] [-Include
<String[]>] [-RequiredServices] [<CommonParameters>]

    -Name <String[]>
        Specifies the service names of services to be retrieved. Wildcards are permitted. By default, this cmdlet gets
        all of the services on the computer.

        Required?                    false
        Position?                    0
        Default value                None
        Accept pipeline input?       True (ByPropertyName, ByValue)
        Accept wildcard characters?  false

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.