Powershell:- Get-ChildItem – Type of Return

Background

It is a lovefest for me to do any work in Powershell.

 

Variable Declaration

Having admitted that I am a 60s love child when it comes to Powershell,  I am not quite a fan of its laissez-faire when it comes to variable declarations.

 

Sample Code

Let us get a list of files on our home directory


<#
    Declare variables
#>
[string] $folder ="";
[string] $log = "";
[object] $fileList =$null;

<#
    Get home directory
#>
$folder=$HOME

<#
    Get list of files from our home directory
#>
$fileList = Get-ChildItem -Path $folder;


<#
    If file list is not null
#>
if ($fileList -ne $null )
{
   
   
    <#
        Display folder name
    #>
    $log = "Folder is {0}" -f $folder;
    
    Write-Host $log            
    
    <#
        Get list of files
    #>
    $fileList

}

 

 

 

Explanation

Variable Declaration

So we have declared three variables.

  1. $folder as a string
  2. $fileList as an object
  3. $log as a string

 

No arguments with our definition of folder and log.

But, declaring $fileList as an object, seems a bit ‘catch all‘.

A man for all seasons so to speak.

 

What is the actual type of $fileList?

 

Outline

Please use GetType to get the actual type of the object.

Code



<#
    Declare variables
#>
[string] $folder ="";
[string] $log = "";

[object] $fileList =$null;

[object] $fileListType = "";

[type]   $fileListType = $null;

[string] $fileListTypeName = "";

[string] $fileListTypeBaseType ="";
 
[boolean] $displayMetadataOnly = $false; 

$displayMetadataOnly = $true;

<#
    Get home directory
#>
$folder=$HOME

<#
    Get list of files from our home directory
#>
$fileList = Get-ChildItem -Path $folder;


<#
    If file list is not null
#>
if ($fileList -ne $null )
{

    Write-Host "";
    
    Write-Host "";
    
    <#
        Display folder name
    #>
    $log = "Folder is {0}" -f $folder;
    
    if ($displayMetadataOnly -eq $false)
    {
        
        Write-Host $log; 
        
    }
 
    <#
        Get type of fileList
    #>
    $fileListType = $fileList.GetType();
    
    $fileListType;
    
    $fileListTypeName = $fileListType.Name;
           
    $fileListTypeBaseType = $fileListType.BaseType;
    
   
    <#
        Display fileList type
    #>
    $log = "The name of the type of fileList is {0}" -f $fileListTypeName;
    
    Write-Host $log;    


    <#
        Display fileList base type
    #>
    $log = "The base type of fileList is {0}" -f $fileListTypeBaseType;
    
    Write-Host $log;
    
    $log = "The base type of fileList is {0}" -f $fileListType.BaseType;
    
    Write-Host $log;    
    
    <#
        Get list of files
    #>
    if ($displayMetadataOnly -eq $false)
    {

        $fileList
    
    }

}



 

Output

Image

Explanation

  1. GetType()
    • Get Object’s type
  2. GetType().Name
    • The name of the returned type is object[]
  3. GetType().BaseType
    • The base type of the returned type is System.Array

 

 

Use the actual type of $fileList?

 

Outline

Please use the actual type of each variable.

Code


<#
    Declare variables
#>
[string] $folder ="";
[string] $log = "";

<#
    Changed $fileList
        from:- [object]
        to:-   [object[]]
#>
#[object] $fileList =$null;
[object[]] $fileList =$null;

<#
    Get home directory
#>
$folder=$HOME

<#
    Get list of files from our home directory
#>
$fileList = Get-ChildItem -Path $folder;


<#
    If file list is not null
#>
if ($fileList -ne $null )
{

    Write-Host "";
    
    Write-Host "";
    
    <#
        Display folder name
    #>
    $log = "Folder is {0}" -f $folder;
    
    Write-Host $log; 
  
    $fileList
    
  
}

 

Source Code Control

GitHub

Repository

https://github.com/DanielAdeniji/fileSystem.fileList/tree/main/powershell

Link

 

Summary

Variable declaration is not required in PowerShell.

Yet, declaring your variables will:-

  1. Further Readability
  2. Deepen your understanding of the API Set
  3. Hopefully, lessen the likelihood of errors
  4. Help your programming chops in this language and other more stringent languages

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s