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
   + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

Open in new window

[code][/get-childitem $path -recurse  | foreach {If ($_.psiscontainer)  {
    $temp = "" | select "Folder", "Access" code]
Who is Participating?
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?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.