Powershell Directory.Name return the trailing separator

powershell

I'm writing my first powershell script:

foreach ($UserDir in Get-ChildItem -Path $NetworkLocation) 
{
    # if the item is a directory, then process it.
    if ($UserDir.Attributes -eq "Directory")
    {
        $Dir = $UserDir.Name
        Remove-Item $Dir +"*" -recurse
    }
}

Will $Dir end with a trailing "\" so I can just add a * to delete all files in that directory?

Best Answer

No it won't supply a trailing slash.

PS C:\> $dirlist=get-childitem c:\
PS C:\> $dirlist[4].name
PerfLogs
PS C:\> 

Nor will it supply full path:

PS C:\> $dirlist=get-childitem C:\windows
PS C:\> $dirlist[3].name 
assembly
PS C:\> $dirlist[3].fullname
C:\Windows\assembly

That is provided through "fullname", which also doesn't provide a trailing slash. However, Remove-Item doesn't need slashes!

In summary, to remove all files in a directory and all files in it's sub-directories, use:

Remove-Item $Dir -recurse

where

$Dir = $UserDir.Fullname