Error on Path with spaces, commas, parenthesis and slash

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

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.

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

$Path = '.'
$Depth = 20
$ExportFolder = "c:\users\mmsho\documents"

Get-ChildItem -Path $Path -Directory -Recurse -Depth $Depth | 
    select LastWriteTime,Fullname | export-csv "$ExportFolder\testoutput.csv"

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…