Error on Path with spaces, commas, parenthesis and slash

Welcome Forums General PowerShell Q&A Error on Path with spaces, commas, parenthesis and slash

Viewing 2 reply threads
  • Author
    • #237409
      Topics: 3
      Replies: 15
      Points: 50
      Rank: Member

      Errors when running code below, how do I bypass spaces, commas, parenthesis and slashes that is comes across in the Paths?

      $StartLevel = 2 # 0 = include base folder, 1 = sub-folders only, 2 = start at 2nd level
      $Depth = 20 # How many levels deep to scan
      $Path = “.” # starting path

      $folders = For ($i=$StartLevel; $i -le $Depth; $i++) {
      $Levels = “\*” * $i
      (Resolve-Path $Path$Levels).ProviderPath | Get-Item | Where PsIsContainer |
      Select FullName

      $Folders.fullname | %{ Get-ChildItem -path $_ -Recurse | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 | Format-Table -Property LastWriteTime,FullName -Autosize | Out-File -Append C:\Users\mmsho\Documents\Testoutput.txt -Encoding UTF8

    • #237418
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      Michelle, welcome to Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code, error messages, sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      What should be the result of this code?

      Regardless of that: format cmdlets should (almost) always be the last element in a pipeline. It does not make sense to pipe the output of Format-Table to Out-File. A suitable option would be to use Export-Csv.

    • #237424
      Topics: 6
      Replies: 17
      Points: 132
      Helping Hand
      Rank: Participant

      Any reason why you can’t do the following?:

      CSV is a cleaner export than a foreach-FormatTable.

      But to answer your question, it’s because (Resolve-Path $Path$Level).Provide.... should be (Resolve-Path "$Path$Levels").Provide...

Viewing 2 reply threads
  • You must be logged in to reply to this topic.