"[" and "]" characters messed up get-childitem

Using PowerShell 4.0, I try to get the size of several directories, and I get very conflicting results between what the windows tell me and what my code says.

This code:

$temp4 = ($folderInfo.rootFolder).fullname
$folderInfo.directories += Get-ChildItem -LiteralPath $temp4 -Recurse -Force -Directory
$folderInfo.directories += $folderInfo.rootFolder
foreach ($dir in $folderInfo.directories)
{
    $temp3 = $dir.fullname
    $temp2 = Get-ChildItem -LiteralPath $temp3 -Force
    $temp = (Get-ChildItem -LiteralPath $dir.fullname -Force -File | Measure-Object -Property length -Sum -ErrorAction SilentlyContinue).Sum
    $folderInfo.totalSize += $temp
}
return $folderInfo

if $folderInfo.rootFolder = D:\sample then I get what I want but if $folderInfo.rootFolder = D:\[sample then I get

Get-ChildItem: . : sample [sample C:\powershell\test.ps1: 55 char: 12 + $temp = (Get-ChildItem $dir.fullname -Force -File | Measure-Object -Property l... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo: InvalidArgument: (:) [Get-ChildItem], ParameterBindingException     + FullyQualifiedErrorId: GetDynamicParametersException, Microsoft.PowerShell.Commands.GetChildItemCommand

, D:\sample - , "[sample". , . $dir.pspath, $dir.fullname .

: , .
: .

+4
1

-LiteralPath -Path, . , V4, -Directory $_.iscontainer:

$folderInfo.directories = 
 Get-ChildItem -LiteralPath $folderInfo.rootFolder -Recurse -Force -Directory 

, Get-ChildItem:

$folderInfo.directories += Get-ChildItem -LiteralPath $folderInfo.rootFolder -Recurse -Force -Directory
    $folderInfo.directories += Get-Item -LiteralPath $folderInfo.rootFolder
    foreach ($dir in $folderInfo.directories)
    {
        $temp2 = Get-ChildItem -LiteralPath $dir.PSPath -Force
        $temp = (Get-ChildItem -LiteralPath $dir.fullname -Force -File | Measure-Object -Property length -Sum -ErrorAction SilentlyContinue).Sum
        $folderInfo.totalSize += $temp
    }
    return $folderInfo
+10

Source: https://habr.com/ru/post/1525690/


All Articles