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)

Alias the aliases (Simple)


Everyone seeks to type less and perform more. To serve this purpose, we have aliases in Windows PowerShell.

Getting ready

In PowerShell Version 3.0, many parameters have been introduced for Get-ChildItem that are very efficient with filesystem drives.

How to do it...

  1. The following command retrieves only system files from the C:\Windows location:

    PS C :\> Get-Childitem –Path C:\Windows -File –System
    
  2. Use its abbreviated form as follows:

    PS C :\> dir -pa C:\Windows -af -as
    

Tip

The -Recurse parameter is also supported with an item that does not have child items, such as C:\Scripts\*.ps1. Previously, in Version 2.0, it was only supported with the container, which has child items.

How it works...

The following are the parameters newly introduced with the Get-ChildItem CMDLET.

  • -Attributes <FileAttributes]>: This parameter retrieves files and folders with the supplied attribute. There are many attributes accepted, such as Archive, Compressed, Device, Directory, Encrypted, Hidden, Normal, NotContentIndexed, Offline, ReadOnly, ReparsePoint, SparseFile, System, and Temporary. We can also use abbreviated forms for the following attributes:

    • D for Directory

    • H for Hidden

    • R for Read-only

    • S for System

    We can use NOT (!), AND (+), and OR (,) operators to combine multiple attributes.

  • -Directory [<SwitchParameter>]: This parameter lists only directories, not files.

  • -File [<SwitchParameter>]: This parameter lists only files, not directories.

  • -Hidden [<SwitchParameter>]: By default, Get-ChildItem retrieves non-hidden files and folders from the specified path. Use the Hidden parameter to only retrieve hidden files in the CMDLET output.

    Tip

    You can use the –Force parameter to retrieve all the (hidden and non-hidden) files in the CMDLET output.

  • -ReadOnly [<SwitchParameter>]: This parameter retrieves files and folders with the read-only attribute.

  • -System [<SwitchParameter>]: This parameter retrieves only system files from the specified path/directory.

There are few aliases available with respect to the Get-ChildItem CMDLET, specified as follows:

  • dir for Get-ChildItem

  • d, ad for Directory

  • af for File

  • h, ah for Hidden

  • ar for ReadOnly

  • as for System

There's more…

There are a few minor enhancements in Version 3.0 for alias mechanisms with respect to the following CMDLETs.

Get-Alias

Until PowerShell Version 2.0, the Get-Alias CMDLET gave output in a hyphenated form. Now, in PowerShell v3.0, Get-Alias displays nonhyphenated lists of alias names. This simplified output is easy to refer to and interpret.

The following is a sample output:

PS C:\> Get-Alias ps, cls
CommandType         Name
-----------         ----
Alias               ps -> Get-Process
Alias               cls -> Clear-Host
PS C:\> Get-Alias | Select-Object Name,DisplayName,Options

The preceding command retrieves a list of aliases available in the current session with three properties: Name, DisplayName, and Options.

By referring to the Option property, you can list out Alias with read-only options.

Note

Select-String is a CMDLET to search for text block in strings and files. In version 3.0, a new alias has been mapped to Select-String, that is, sls. It is similar to grep in Unix terminology.

Import-Alias

By default, for security reasons, Import-Alias does not overwrite existing aliases. In other words, it doesn't modify read-only alias names by importing aliases from other sessions. To forcefully overwrite the existing aliases in the current session, use the –Force parameter with the Import-Alias CMDLET.

Get-Acl

There are a few parameters introduced in Windows PowerShell Version 3.0:

  • -AllCentralAccessPolicies [<SwitchParameter>]: This parameter retrieves information about all the central access policies present on the local computer. In Windows Server 2012, administrators have the facility to set up central access policies for users and groups using Active Directory and Group Policy.

  • -InputObject <PSObject>: This parameter helps us get the security descriptor for those objects that do not have a defined path. It accepts the PSObject type.

  • -LiteralPath <String[]>: This parameter is useful when we need to explicitly provide the true path for the objects to retrieve security descriptors for them. It doesn't accept wildcard characters.

Note

With PowerShell Version 3.0, the Get-Random CMDLET supports 64-bit integer values; previously, in Version 2.0, all values were cast to System.Int32.