Solving The PowerShell File Class Challenge
In this issue:
- Overview
- Introduction
- Defining the Class Properties
- Class Constructors
- Adding Methods
- Categorizing
- Emojis
- Extending the Object
- Creating a Helper Function
- PSFileInfo
- Summary
Overview
My solution for the October 2026 PowerShell scripting challenge where you were tasked with creating a PowerShell class describing a file. Remember that the goal of the challenge is not the final result but what you learn in developing a solution.
Introduction
Let's see how you did with last month's PowerShell scripting challenge. I wanted to see if you could write PowerShell code that would describe a file with the following properties:
- the file path
- file name
- file size
- extension
- creation time
- last modified time
- the folder name
- if the file is readonly
- if the file is hidden
- if it is a system file
- the drive root, i.e. C:\
- the names of any alternate data streams other than the default $DATA
- the computer name
If the file is linked, you should capture that including the resolved file name can capture the linked values for size and time stamps. You should extend the class to show a script properties that reflects the creation and modified ages.
For extra credit I wanted to see if you could categorize the file based on its extension. And for super-duper extra credit, add a method that will compress the file into a zip file. Finally, to really show off, create formatting that will use an emoji to reflect the category.
I recognize that you could extend the System.IO.FileInfo class, but I wanted to give you an opportunity to get your hands dirty creating a PowerShell class. Remember that the goal of the challenge is not the final result but what you learn in developing a solution.
Defining the Class Properties
To create a PowerShell class all you need at a minimum is the class name and the desired properties.
class PSFileInfo {
[string]$Path
[string]$Name
[int64]$Size
[string]$Extension
[DateTime]$Created
[DateTime]$Modified\
...
}
You should assign a type name for each property. Part of the challenge was to categorize the file, so I'm going to also define an enum.
enum PSFileCategory {
other
text
binary
executable
media
image
datafile
powershell
document
coding
archive
}
When defining an enum I recommend you put your default value first. Now, I can use this enum in the class.
class PSFileInfo {
[string]$Path
[string]$Name
[int64]$Size
[string]$Extension
[DateTime]$Created
[DateTime]$Modified
[PSFileCategory]$Category #my enum based property
[bool]$IsLinked #these will default to False
[bool]$IsReadOnly
[bool]$IsSystem
[bool]$IsHidden
[string]$Folder
[string]$Root
[string]$Computername = [System.Environment]::MachineName #Set a default value
...
}
One thing you can do with a class is to make properties hidden. You can't make them read-only, but you can hide them from casual viewing. If the user knows they exist, they can reference and set them. You too, can modify them. In my class, I have a few properties that I want to hide.
class PSFileInfo {
[string]$Path
[string]$Name
[int64]$Size
[string]$Extension
[DateTime]$Created
[DateTime]$Modified
[PSFileCategory]$Category
[bool]$IsLinked
[bool]$IsReadOnly
[bool]$IsSystem
[bool]$IsHidden
[string]$Folder
[string]$Root
[string]$Computername = [System.Environment]::MachineName
hidden [object[]]$Streams
hidden [string]$ResolvedPath
hidden [string]$CategoryEmoji
}
Class Constructors
By default, you can create a new instance of the class using New-Object, or invoking the new() static method.
[PSFileInfo]::new()
However, I want to create a constructor based on the path of an existing file object. The path will be a required method parameter.
[PSFileInfo]::new([string]$Path)
Here's how this looks in the class definition.
class PSFileInfo {
<# properties #>
PSFileInfo([string]$Path) {
#use LiteralPath to allow for special characters
$file = Get-Item -LiteralPath $Path -Force
$this.Path = $file.FullName
$this.Folder = Split-Path -Path $this.path -Parent
$this.Root = $file.PSDrive.Root
$this.IsLinked = (Test-Path -LiteralPath $file.LinkTarget -ea SilentlyContinue) ? $True : $false
$this.Name = $file.Name
$this.Extension = $file.Extension
#need to populate the remaining properties
}
} #close class
It is up to you to define the object properties. In the constructor, use $this to reference the instance of the object.
Adding Methods
You could define the remaining properties in the constructor. However, I realized I might want to refresh the object and get update values like size and last write time. Rather than duplicate code, I can create a class method and call it from the constructor.
#need to populate the remaining properties
$this.Refresh()
I tend to define class methods after properties. The Refresh method needs a return type.
[void]Refresh() {
if (Test-Path -LiteralPath $this.Path) {
$file = Get-Item -LiteralPath $this.Path -Force
#get these settings on the file
$this.IsReadOnly = $file.IsReadOnly
$this.IsSystem = $file.Attributes -contains 'System'
$this.IsHidden = $file.Attributes -contains 'Hidden'
$this.ResolvedPath = $file.ResolvedTarget
$this.Categorize()
$this.SetCategoryEmoji()
if ($this.ResolvedPath -ne $this.Path ) {
if (Test-Path $this.ResolvedPath) {
#get the linked file
$file = Get-Item -LiteralPath $this.ResolvedPath -Force
}
else {
Write-Warning "The resolved file path $($script:file.Resolved) cannot be found on $($this.Computername)"
}
}
#get these settings from the resolved file which might be a link
$this.Size = $file.Length
$this.Created = $file.CreationTime
$this.Modified = $file.LastWriteTime
$this.Streams = ($global:IsWindows) ? ((Get-Item -LiteralPath $this.ResolvedPath -Stream *).where({ $_.Stream -NotMatch ':\$DATA' }) | Select-Object Stream, Length) : $null
}
else {
Write-Warning "The file $($this.Path) cannot be found on $($this.Computername)"
}
}
Using [void] indicates the method doesn't write a result to the pipeline. All it is doing is updating properties on the instance of the object.
PS C:\> $f = [psfileinfo]::new($profile.CurrentUserAllHosts)
PS C:\> $f
Path : C:\Users\Jeff\Documents\PowerShell\profile.ps1
Name : profile.ps1
Size : 1908
Extension : .ps1
Created : 12/19/2024 9:12:44 AM
Modified : 8/24/2026 5:04:33 PM
Category : powershell
IsLinked : True
IsReadOnly : False
IsSystem : False
IsHidden : False
Folder : C:\Users\Jeff\Documents\PowerShell
Root : C:\
Computername : PROSPERO