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.
- $folder as a string
- $fileList as an object
- $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
- GetType()
- Get Object’s type
- GetType().Name
- The name of the returned type is object[]
- 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
Summary
Variable declaration is not required in PowerShell.
Yet, declaring your variables will:-
- Further Readability
- Deepen your understanding of the API Set
- Hopefully, lessen the likelihood of errors
- Help your programming chops in this language and other more stringent languages