Guest User

Get-DirectoryItem.psm1

a guest
Jan 26th, 2024
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. FAST alternative to Get-ChildItem when one only wants to see the contents of a directory
  4.  
  5. .DESCRIPTION
  6. Much less powerful but much, MUCH faster alternative to Get-ChildItem.
  7. Meant to be use in terminal by user when they just want to SEE the contents of a directory
  8.  
  9.  
  10.  
  11. .outputS
  12. Default: A simple Array of [System.IO.FileInfo]+[System.IO.DirectoryInfo] for each element in the directory
  13. with -Raw: A simple Array of [String] representing the Full Path of each element in the directory
  14.  
  15. #>
  16. function Get-DirectoryItem {
  17.     [CmdletBinding(DefaultParameterSetName = 'BaseSet')]
  18.     [Alias('Get-Dir', 'GD')]
  19.     param (
  20.         # Directory to list
  21.         [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, ValueFromRemainingArguments, ParameterSetName = 'BaseSet' )]
  22.         [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, ValueFromRemainingArguments, ParameterSetName = 'ShowAll')]
  23.         [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, ValueFromRemainingArguments, ParameterSetName = 'ShowSome')]
  24.         [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, ValueFromRemainingArguments, ParameterSetName = 'ExcludeAttributes')]
  25.         [PSDefaultValue(Help = '$PWD (=Present Working Directory)')]
  26.         [ValidateScript({ Test-Path -PathType Container -Path $_ }, ErrorMessage = { '"{0}" non esiste o non รจ una Directory' })]
  27.         [string]$Path = $PWD,
  28.  
  29.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'BaseSet' )]
  30.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowAll')]
  31.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowSome')]
  32.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ExcludeAttributes')]
  33.         [PSDefaultValue(Value = '*', Help = "'*' (=everything); NOT A A REGEX")]
  34.         [string]$Pattern = '*',
  35.  
  36.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowAll')]
  37.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowSome')]
  38.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ExcludeAttributes')]
  39.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'BaseSet' )]
  40.         [switch]$Recurse,
  41.  
  42.         # Returns a [String] Array containing only the Full Path of the items in the directory instead of a [System.IO.FileInfo] Array
  43.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'BaseSet' )]
  44.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowAll')]
  45.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowSome')]
  46.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ExcludeAttributes')]
  47.         [switch]$Raw,
  48.  
  49.         # At the end of the outputgd, adds a WRITE-HOST line with the total number of the item in the directory
  50.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'BaseSet' )]
  51.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowAll')]
  52.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowSome')]
  53.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ExcludeAttributes')]
  54.         [switch]$TotalCount,
  55.  
  56.  
  57.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ExcludeAttributes')]
  58.         [ValidateSet(
  59.             'Archive',
  60.             'Encrypted',
  61.             'Normal' ,
  62.             'ReadOnly',
  63.             'Temporary',
  64.             'Compressed',
  65.             'Hidden' ,
  66.             'NoScrubData' ,
  67.             'ReparsePoint',
  68.             'Device' ,
  69.             'IntegrityStream' ,
  70.             'NotContentIndexed' ,
  71.             'SparseFile',
  72.             'Directory' ,
  73.             'Offline' ,
  74.             'System'
  75.         )]
  76.         [ValidateCount(1, 16)]
  77.         [string[]]$ExcludeAttributes,
  78.  
  79.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowSome')]
  80.         [switch]$ShowSystem,
  81.  
  82.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowSome')]
  83.         [switch]$ShowHidden,
  84.        
  85.         [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ShowAll')]
  86.         [Alias('Force')]
  87.         [switch]$ShowAll
  88.  
  89.        
  90.  
  91.     )
  92.    
  93.     DynamicParam {
  94.         if ($Recurse) {
  95.  
  96.             $parameterAttribute = [System.Management.Automation.ParameterAttribute]::new()
  97.             $parameterAttribute.ValueFromPipelineByPropertyName = $true
  98.  
  99.             $parameterValidation = [System.Management.Automation.ValidateRangeAttribute]::new(1, [Int32]::MaxValue)
  100.            
  101.             $attributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
  102.             $attributeCollection.Add($parameterAttribute)
  103.             $attributeCollection.Add($parameterValidation)
  104.            
  105.             $dynParam1 = [System.Management.Automation.RuntimeDefinedParameter]::new('RecurseMax', [int32], $attributeCollection)
  106.  
  107.             $paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
  108.             $paramDictionary.Add('RecurseMax', $dynParam1)
  109.            
  110.             return $paramDictionary
  111.         }
  112.     }
  113.  
  114.     begin {
  115.        
  116.     }
  117.  
  118.     process {
  119.        
  120.         $Attributes = [System.IO.EnumerationOptions]::new()
  121.  
  122.         if ($ExcludeAttributes) {
  123.             $Attributes.attributesToSkip = $ExcludeAttributes | Select-Object -Unique
  124.         }
  125.         elseif ($ShowAll) {
  126.             $Attributes.attributesToSkip = 'None'
  127.         }
  128.         else {
  129.             if ($ShowHidden) {
  130.                 $Attributes.attributesToSkip -= 'Hidden'
  131.             }
  132.             if ($ShowSystem) {
  133.                 $Attributes.attributesToSkip -= 'System'
  134.             }
  135.         }
  136.        
  137.         if ($Recurse) {
  138.             $Attributes.RecurseSubdirectories = $true
  139.             if ($PSBoundParameters['RecurseMax']) {
  140.                 $Attributes.MaxRecursionDepth = $PSBoundParameters['RecurseMax']
  141.             }
  142.         }
  143.        
  144.         $Path = Resolve-Path -Path $Path.trim()
  145.  
  146.         if ($Raw) {
  147.             Write-Debug 'here'
  148.             # $Lista
  149.             [System.IO.Directory]::GetFileSystemEntries($Path, "$Pattern", $Attributes )
  150.         }
  151.         else {
  152.             Write-Debug 'there'
  153.             $DateTimePattern = 'yyyy/MM/dd  hh:mm:ss'
  154.             ([System.IO.Directory]::GetFileSystemEntries($Path, "$Pattern", $Attributes )).ForEach({
  155.                     [PSCustomObject]@{
  156.                         'Size(Byte)'                                  = ([System.IO.FileInfo]$_).Length
  157.                         'LastWrite'.PadRight($DateTimePattern.Length) = ([System.IO.FileInfo]$_).LastWriteTime.ToString($DateTimePattern)
  158.                         'Name'                                        = ($Recurse) ?  [System.IO.Path]::GetRelativePath($Path, $_) : [System.IO.Path]::GetFileName($_)
  159.                     }
  160.             })
  161.         }
  162.    
  163.     }
  164.  
  165.     end {
  166.         if ($TotalCount) {
  167.             Write-Host $([System.Environment]::NewLine) -NoNewline
  168.             Write-Host ("Total Count for '{0}': {1}" -f $Path, [System.IO.Directory]::GetFileSystemEntries($Path, "$Pattern", $Attributes ).count)
  169.             Write-Host $([System.Environment]::NewLine)
  170.         }
  171.     }
  172. }
  173.  
Advertisement
Add Comment
Please, Sign In to add comment