Book Image

Instant Windows PowerShell Guide

By : Harshul Patel
Book Image

Instant Windows PowerShell Guide

By: Harshul Patel

Overview of this book

Windows PowerShell has become a booming scripting language over the last couple of years. It has extensive support with an ample number of vendor products, providing a standardized platform for automation and administration. It has massive support for all Microsoft products which creates a layer that can easily automate everything. In the latest version, the PowerShell team has introduced much more functionality with thousands of CMDLETs, part of various modules.This book is a quick reference guide to enable you to get the most out of the latest Windows PowerShell techniques. In this book, you will find new enhancements in the latest version of PowerShell with some helpful examples. This book enables you to quickly move from older versions of PowerShell to Version 3.0 and Version 4.0.This practical, example-oriented book helps you to overcome the difficulty of using and discovering CMDLETs by providing precise information about everything that has been newly introduced in the latest version of Windows PowerShell. It also focuses on the new configuration management system with the help of DSC as a new feature of Windows PowerShell v4.0.You will learn how to use the newly introduced CMDLETs and parameters to perform daily routine tasks. You will also learn how to administer the servers remotely and maintain persistent sessions to provide continuity. You will gain an insight into writing efficient scripts by using various parameters, snippets, and workflows to gain more productivity. You will also be introduced to various modules like CimCmdlets, PSScheduledJob, PSDesiredStateConfiguration, and so on in order to enhance your scripts with the latest instrumentation. Finally this book will make you aware of the capabilities of PowerShell v4.0 and how to fully leverage the functionality introduced in the new version.
Table of Contents (7 chapters)

Operate the data (Intermediate)


Data Operations are critical routine administrative tasks and, with the release of Windows PowerShell Version 3.0, we have made some improvements regarding the handling of data in the console.

Getting ready

There are a few new operators introduced in PowerShell Version 3.0. For example, the In and NotIn operators.

How to do it...

Try executing the following code:

  1. The following command checks the availability of 1 in the reference set and returns True in this case:

    PS C :\> 1 -In (1,2,3)
    True
    
  2. The following command checks for an exact counter match for the Admin keyword in the reference set and returns True in this case:

    PS C :\> "Admin" -NotIn "Administrator"
    True
    

How it works...

The following table mentions the syntaxes for these operators:

Operator

Syntax

Description

-In

<Test-value> -In <Reference-values>

This operator returns a Boolean value as output. If any test value is present in the reference values set, it returns True, else False.

-NotIn

<Test-value> -NotIn <Reference-values>

This operator also returns a Boolean value as output. If any test value is not present in the reference values set, it returns True, else False.

There's more…

There are a few new parameters introduced with the following CMDLETs:

Get-Content

The Get-Content CMDLET retrieves content from a specified file.

  • -Tail <Int32>: To retrieve the number of lines from the file, use the Tail parameter with the Get-Content CMDLET. It retrieves the number of lines supplied to this parameter. For example:

    PS C :\> Get-Content -Path C:\PSTest.txt -Tail 10
    

    The preceding command statement retrieves the last 10 lines from C:\PSTest.txt.

Tip

You can use the –Last instead of the Tail parameter; it is an alias of this parameter.

Tee-Object

The Tee-Object CMDLET stores output in a file or variable and throws to the pipeline.

  • -Append [<SwitchParameter>]: If any file already exists and you try to supply it with Tee-Object, it overrides the content by default. To avoid this, you can use the –Append parameter with the Tee-Object CMDLET. For example:

    PS C:\>Get-ChildItem -Path C:\PSBooks -Recurse | Tee-Object -File C:\BookList.txt –Append
    

    The preceding command statement gets the list of PowerShell books and appends it to BookList.txt.