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...
The following command retrieves only system files from the
C:\Windows
location:PS C :\> Get-Childitem –Path C:\Windows -File –System
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 asArchive
,Compressed
,Device
,Directory
,Encrypted
,Hidden
,Normal
,NotContentIndexed
,Offline
,ReadOnly
,ReparsePoint
,SparseFile
,System
, andTemporary
. We can also use abbreviated forms for the following attributes:D
forDirectory
H
forHidden
R
forRead-only
S
forSystem
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 theHidden
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
forGet-ChildItem
d
,ad
forDirectory
af
forFile
h
,ah
forHidden
ar
forReadOnly
as
forSystem
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 thePSObject
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
.