Pergunta

I'm still new to PowerShell and trying to figure the best way to get the size of individual Itunes folders on our server. There are multiple folders and they are all located in the user profiles. This is the script i have so far but it doesnt show all the individual folders.

#Gets a list of all itunes folders 
$dirOfItunes = dir  -Recurse -Filter "iTunes Media" -ErrorAction silentlycontinue

Write-Host "list of itunes folders found: " $dirOfItunes 
ForEach ($i in $dirOfItunes) 
{
   $UserItuneFolder = (Get-ChildItem -Recurse | Measure-Object -property length -sum)

   Write-Host "details of Itunes folders found:  " Write-Host $UserItuneFolder.get_Sum()

   write-host "folder name:  " $i.FullName

   "size " + "{0:N2}" -f ($UserItuneFolder.sum / 1MB) + " MB"
}
Foi útil?

Solução

You forgot the $i (in the foreach loop) after get-childitem (you want to know the total size of $i, right?)

This should do what you want:

#Gets a list of all itunes folders 
$dirOfItunes = dir  -Recurse -Filter "iTunes Media" -ErrorAction silentlycontinue

Write-Host "list of itunes folders found: " $dirOfItunes 
ForEach ($i in $dirOfItunes) 
{
   $UserItuneFolder = (Get-ChildItem $i.fullname -Recurse | Measure-Object -property length -sum)
   Write-Host "details of Itunes folders found:  " ([int] $UserItuneFolder.sum)
   write-host "folder name:  " $i.FullName
   "size " + "{0:N2}" -f ($UserItuneFolder.sum / 1MB) + " MB"
}

Outras dicas

Not sure if im doing the right thing posting this as a answer because theres not enough room in the comments but this is the final code i used. Thanks jon Z for your help in solving the issue :-)

#Gets a list of all itunes folders 
$dirOfItunes = dir L:\Users\FolderRedirections -Recurse -Filter "iTunes Media" -ErrorAction silentlycontinue

#print list of folders found to the screen 
Write-Host "list of itunes folders found: "
ForEach ($A in $dirOfItunes) 
    {
    write-host "    " $A.fullname 
    }

#Get the total size of each folder 
ForEach ($i in $dirOfItunes) 
{
   $UserItuneFolder = (Get-ChildItem $i.fullname -Recurse | Measure-Object -property length -sum)
   Write-Host "Size of folders in bytes:  " ([int] $UserItuneFolder.sum)
   write-host "Folder name:  " $i.FullName
   "size of folder: " + "{0:N2}" -f ($UserItuneFolder.sum / 1MB) + " MB"

}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top