Prevent Error generated on Powershell script

I have executed the powershell with a statement like below. Occasionally, it generate a error when the path is longer than 248 chars. Is it possible to get rid of the error message ?

et-childitem : The specified path, file name, or both are too long. The fully qualified file name must be less than
60 characters, and the directory name must be less than 248 characters.
t C:\scripts\ListFolders.ps1:7 char:1
 get-childitem $path -recurse  | foreach {If ($_.psiscontainer)  {
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : ReadError: (D:\dept-home\IR...t Summit London:String) [Get-ChildItem], PathTooLongExcept
  ion
   + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

Open in new window


[code][/get-childitem $path -recurse  | foreach {If ($_.psiscontainer)  {
    $temp = "" | select "Folder", "Access" code]
AXISHKAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daryl BamforthTechnical ExpertCommented:
Yes, you could add
-erroraction silentlycontinue

Open in new window

If you really don't care about it and just want to ignore it.

However what you should do is use a try statement. Then add a catch for that specific error.

Try {
code where error is being generated
}
catch [system error state] {
use this to catch a specific error that is being generated. Ideally this is the one you want as it handles an expected error.

}
catch {
use this for unexpected errors, log the last error generated with $error[0] so you can handle the error correctly in future.
}
finally {
This happens in a success or failure of the try statement, it can just be left blank if really not necessary.
}

Open in new window


This means that you can correctly handle the error when it occurs (logging it for instance), and it won't stop the rest of the script. If you have directories that are exceeding the character limit you will have difficulties managing the data as things like backups will not be working for areas the OS cannot address.

I assume you have users mapping the drive lower down the tree?
3

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.