PowerShell Object Property Strangeness Between 5.1 and 7.1
Hello!
Got caught out with a bug that had me confused for quite some time earlier this week; it seemed that on some peoples machines we’d get an error about a file not existing, when we knew the file had to exist. I say we knew it existed because we ran a ``Get-ChildItem``` on a folder to get the paths, and then loop through each file and process them. A pithy version of the script is below:
$PSVersionTable
$csvs= Get-ChildItem -LiteralPath "C:\Users\RichieLee\Downloads\f1db_csv"
foreach($csv in $csvs){
Write-Host $csv
}
Quite by chance I ran the script on 5.1 and then 7.1 just to see what happened. Running this in PowerShell 5.1 returns only the file name…
yet running in PowerShell 7.1 we get the equivalent of the fullname property on the object:
I double checked that those that were getting the error were running 5.1, so by changing to use the FullName
property on the object then it would run successfully irrespective of which version of PowerShell was being used.
OK, problem solved, but why is this happening? I stil don’t have the answer for that question, but whilst both 5.1 and 7.1 versions of the cmdlets return a type of output
, clearly there is a difference between them. By checking GetType()
we can see there is a difference in the version assemblies (version 4 in 5.1 and version 5 in PowerShell 7.1). There must have been something across these versions that caused this change.