Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- FAST alternative to Get-ChildItem when one only wants to see the contents of a directory
- .DESCRIPTION
- Much less powerful but much, MUCH faster alternative to Get-ChildItem.
- Meant to be use in terminal by user when they just want to SEE the contents of a directory
- .outputS
- Default: A simple Array of [System.IO.FileInfo]+[System.IO.DirectoryInfo] for each element in the directory
- with -Raw: A simple Array of [String] representing the Full Path of each element in the directory
- #>
- function Get-DirectoryItem {
- [CmdletBinding(DefaultParameterSetName = 'BaseSet')]
- [Alias('Get-Dir', 'GD')]
- param (
- # Directory to list
- [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, ValueFromRemainingArguments, ParameterSetName = 'BaseSet' )]
- [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, ValueFromRemainingArguments, ParameterSetName = 'ShowAll')]
- [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, ValueFromRemainingArguments, ParameterSetName = 'ShowSome')]
- [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, ValueFromRemainingArguments, ParameterSetName = 'ExcludeAttributes')]
- [PSDefaultValue(Help = '$PWD (=Present Working Directory)')]
- [ValidateScript({ Test-Path -PathType Container -Path $_ }, ErrorMessage = { '"{0}" non esiste o non รจ una Directory' })]
- [string]$Path = $PWD,
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'BaseSet' )]
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowAll')]
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowSome')]
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ExcludeAttributes')]
- [PSDefaultValue(Value = '*', Help = "'*' (=everything); NOT A A REGEX")]
- [string]$Pattern = '*',
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowAll')]
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowSome')]
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ExcludeAttributes')]
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'BaseSet' )]
- [switch]$Recurse,
- # Returns a [String] Array containing only the Full Path of the items in the directory instead of a [System.IO.FileInfo] Array
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'BaseSet' )]
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowAll')]
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowSome')]
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ExcludeAttributes')]
- [switch]$Raw,
- # At the end of the outputgd, adds a WRITE-HOST line with the total number of the item in the directory
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'BaseSet' )]
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowAll')]
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowSome')]
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ExcludeAttributes')]
- [switch]$TotalCount,
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ExcludeAttributes')]
- [ValidateSet(
- 'Archive',
- 'Encrypted',
- 'Normal' ,
- 'ReadOnly',
- 'Temporary',
- 'Compressed',
- 'Hidden' ,
- 'NoScrubData' ,
- 'ReparsePoint',
- 'Device' ,
- 'IntegrityStream' ,
- 'NotContentIndexed' ,
- 'SparseFile',
- 'Directory' ,
- 'Offline' ,
- 'System'
- )]
- [ValidateCount(1, 16)]
- [string[]]$ExcludeAttributes,
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowSome')]
- [switch]$ShowSystem,
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowSome')]
- [switch]$ShowHidden,
- [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowAll')]
- [Alias('Force')]
- [switch]$ShowAll
- )
- DynamicParam {
- if ($Recurse) {
- $parameterAttribute = [System.Management.Automation.ParameterAttribute]::new()
- $parameterAttribute.ValueFromPipelineByPropertyName = $true
- $parameterValidation = [System.Management.Automation.ValidateRangeAttribute]::new(1, [Int32]::MaxValue)
- $attributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
- $attributeCollection.Add($parameterAttribute)
- $attributeCollection.Add($parameterValidation)
- $dynParam1 = [System.Management.Automation.RuntimeDefinedParameter]::new('RecurseMax', [int32], $attributeCollection)
- $paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
- $paramDictionary.Add('RecurseMax', $dynParam1)
- return $paramDictionary
- }
- }
- begin {
- }
- process {
- $Attributes = [System.IO.EnumerationOptions]::new()
- if ($ExcludeAttributes) {
- $Attributes.attributesToSkip = $ExcludeAttributes | Select-Object -Unique
- }
- elseif ($ShowAll) {
- $Attributes.attributesToSkip = 'None'
- }
- else {
- if ($ShowHidden) {
- $Attributes.attributesToSkip -= 'Hidden'
- }
- if ($ShowSystem) {
- $Attributes.attributesToSkip -= 'System'
- }
- }
- if ($Recurse) {
- $Attributes.RecurseSubdirectories = $true
- if ($PSBoundParameters['RecurseMax']) {
- $Attributes.MaxRecursionDepth = $PSBoundParameters['RecurseMax']
- }
- }
- $Path = Resolve-Path -Path $Path.trim()
- if ($Raw) {
- Write-Debug 'here'
- # $Lista
- [System.IO.Directory]::GetFileSystemEntries($Path, "$Pattern", $Attributes )
- }
- else {
- Write-Debug 'there'
- $DateTimePattern = 'yyyy/MM/dd hh:mm:ss'
- ([System.IO.Directory]::GetFileSystemEntries($Path, "$Pattern", $Attributes )).ForEach({
- [PSCustomObject]@{
- 'Size(Byte)' = ([System.IO.FileInfo]$_).Length
- 'LastWrite'.PadRight($DateTimePattern.Length) = ([System.IO.FileInfo]$_).LastWriteTime.ToString($DateTimePattern)
- 'Name' = ($Recurse) ? [System.IO.Path]::GetRelativePath($Path, $_) : [System.IO.Path]::GetFileName($_)
- }
- })
- }
- }
- end {
- if ($TotalCount) {
- Write-Host $([System.Environment]::NewLine) -NoNewline
- Write-Host ("Total Count for '{0}': {1}" -f $Path, [System.IO.Directory]::GetFileSystemEntries($Path, "$Pattern", $Attributes ).count)
- Write-Host $([System.Environment]::NewLine)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment