Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How Powershell Handles an Invalid attribute

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading