Powershell Directory.Name返回尾部分隔符?

我在写我的第一个PowerShell脚本:

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 } } 

$ Dir会以“\”结尾,所以我可以添加一个*来删除该目录中的所有文件?

不,它不会提供结尾的斜线。

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

它也不会提供完整的path:

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

这是通过“全名”提供的,也不提供结尾的斜线。 但是,删除项不需要斜杠!

总之,要删除目录中的所有文件及其子目录中的所有文件,请使用:

 Remove-Item $Dir -recurse 

哪里

 $Dir = $UserDir.Fullname