Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Windows PowerShell Guide

You're reading from   Instant Windows PowerShell Guide Enhance your knowledge of Windows PowerShell and get to grips with its latest features

Arrow left icon
Product type Paperback
Published in Nov 2013
Publisher Packt
ISBN-13 9781849686785
Length 86 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Harshul Patel Harshul Patel
Author Profile Icon Harshul Patel
Harshul Patel
Arrow right icon
View More author details
Toc

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.

You have been reading a chapter from
Instant Windows PowerShell Guide
Published in: Nov 2013
Publisher: Packt
ISBN-13: 9781849686785
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image