I am expecting below code to throw an error..since Get-Childitem does not have Lastaccessed
try{
Get-ChildItem | Where-Object { -not $_.PsIsContainer -and $_.lastaccessed -lt $date} -ErrorAction stop
Write-Host "hello:"
}
catch{
Write-Host $($_)
}
But to my surprise,it does not
i did try to search,but cant find any satisfactory answers
>Solution :
You’d need to use Set-StrictMode for this to happen, more specifically version 2.0 or above:
Set-StrictMode -Version 2.0
If you look at the -Version parameter details you’d see:
2.0
- Prohibits references to uninitialized variables. This includes uninitialized variables in strings.
- Prohibits references to non-existent properties of an object.
- Prohibits function calls that use the syntax for calling methods.