Powershell - The path is not of a legal form



  • So in my on-going project to rid our files of special characters I got a new error, specifically The path is not of a legal form. Anyone have any ideas on what this actually means?

    In looking at $error[0] it just repeats the limited error message and doesn't specify where the error issue (or what file or folder) is messed up.



  • In getting the verbose logging I have this

    System.
    Management.Automation.CmdletProviderInvocationException: The path is not of a legal form. ---> System.ArgumentException: The path is not of a legal form.
       at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
       at System.IO.Path.InternalGetDirectoryName(String path)
       at Microsoft.PowerShell.Commands.FileSystemProvider.RenameItem(String path, String newName)
       at System.Management.Automation.SessionStateInternal.RenameItem(CmdletProvider providerInstance, String path, String newName, CmdletProviderContext context)
       --- End of inner exception stack trace ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
       at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAst
    s, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
       at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)]
    

    Still not super helpful, unless it's saying that a file path is too long - but which one?



  • Hey Dustin, you may already have this code in place somewhere, but what about piping the currently worked on file/path via Write-Debug so that it shows this info or, at the least, the file directly prior to it?

    Also, assuming you're removing special characters and not replacing them with something else, is it possible that there is a file that contains entirely these characters and when they're removed there is no file name?



  • @manxam said in Powershell - The path is not of a legal form:

    Hey Dustin, you may already have this code in place somewhere, but what about piping the currently worked on file/path via Write-Debug so that it shows this info or, at the least, the file directly prior to it?

    Well I could probably do this, I'd rather not as everything would get dumped to the console as this is running. Which would create scroll-hell.

    @manxam said in Powershell - The path is not of a legal form:

    Also, assuming you're removing special characters and not replacing them with something else, is it possible that there is a file that contains entirely these characters and when they're removed there is no file name?

    The likelihood of this is very very small, but yes I am replacing all stupid special characters with a null space (delete essentially), so while possible not in an practical sense that would ever occur.



  • @DustinB3403 said in Powershell - The path is not of a legal form:

    Well I could probably do this, I'd rather not as everything would get dumped to the console as this is running. Which would create scroll-hell.

    That is the point of debugging.. once you have worked it out, then you remove it.



  • @JaredBusch said in Powershell - The path is not of a legal form:

    @DustinB3403 said in Powershell - The path is not of a legal form:

    Well I could probably do this, I'd rather not as everything would get dumped to the console as this is running. Which would create scroll-hell.

    That is the point of debugging.. once you have worked it out, then you remove it.

    As always your words ring true.



  • @DustinB3403 : That's the reason I suggested Write-Debug (or Write-Verbose) so that this would not show up on the console by default.



  • @manxam said in Powershell - The path is not of a legal form:

    @DustinB3403 : That's the reason I suggested Write-Debug (or Write-Verbose) so that this would not show up on the console by default.

    I'll give write-verbose -message "my msg" -verbose a go and see what happens.



  • I think I may have found a quicker/more clean means of doing this with just

    Get-ChildItem -Path "Drive-Letter" -Recurse -Force

    It prints everything out to the console or to a file for review, than you just have to read it.


Log in to reply