My script giving error "Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters." and "Measure-Object : The property "length" cannot be found in the input for any objects". It supposed to create a text file with list of folders arranged by size. However,I get errors above and some folders get size 0.0 Mb. Do you have any suggestions?
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
Function Get-FolderName
{
Param (
$PathName
)
$Array=@('\')
ForEach ($Folder in $PathName)
{
$a=[string]$Folder
$a=$a.split("\")[-1]
$a=$a-replace '["}"]',''
$Array+=$a
}
ForEach ($Folder in $Array)
{
$colItems = (Get-ChildItem $directorypath\$Folder -recurse -ErrorAction Inquire | Measure-Object -property length -sum -ErrorAction Inquire )
$colItems ="{0:N2}" -f ($colItems.sum / 1MB)
$colItems =[String]$colItems-replace '[","]',''
#$colItems =[String]$colItems-replace '["."]',''
$colItems = [float]$colItems
$colItemsGB = [float]($colItems /1024)| % { '{0:0.##}' -f $_ }
#$colItems=(Get-ChildItem $directorypath\$Folder -Recurse | Measure-Object Length -Sum)
[PSCustomObject]@{
Folder = $Folder;
Size_MB=$colItems
Size_GB=$colItemsGB
}
}
}
Function Check-FileName{
Param (
$PathName
)
$b=[string]$directorypath.split("\")[-1]
if(Test-Path $directorypath\$b.txt)
{
Clear-Content $directorypath\$b.txt
$content = Get-FolderName $PathName
return $content|Sort-Object -property Size_MB -Descending | out-file $b".txt"
}
else {
$content = Get-FolderName $PathName
return $content |Sort-Object -property Size_MB -Descending| out-file $b".txt"
}
}
$aa = dir -Directory | Select FullName
Check-FileName $aa