get-childitem : Cannot find path ' ' because it does not exist - powershell

I am trying to make a script that can report on a folder size for a specified folder.
I keep getting this error after I put in the folder name and I know the folder exists.
Also if I don't get this error then I always get 0 MB.
get-childitem : Cannot find path 'C:\WINDOWS\system32\downloads' because it does not exist.
At C:\Users\Erik\Desktop\powershell script.ps1:58 char:12
+ $folders = get-childitem $startfolder | where{$_.PSiscontainer -eq "T ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\WINDOWS\system32\downloads:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound, Microsoft.PowerShell.Commands.GetChildItemCommand
Code
$startfolder = Read-Host -Prompt 'Please enter the folder name:'
#check that input is not empty
if([string]::IsNullOrEmpty($startfolder)) {
Write-Host "folder name is NULL or EMPTY"
} else {
$folders = get-childitem $startfolder | where {$_.PSiscontainer -eq "True"}
"folder Name`tfolder Size (MB)"
foreach ($fol in $Folders) {
$colItems = (Get-ChildItem $fol.fullname -recurse | Measure-Object -property length -sum)
$size = "{0:N2}" -f ($colItems.sum / 1MB)
"$($fol.name)`t$size"
}
}

so.. My power-shell is a bit terrible but I've got this working for me:
$startfolder = Read-Host -Prompt 'Please enter the folder name:'
#check that input is not empty
if([string]::IsNullOrEmpty($startfolder)) {
Write-Host "folder name is NULL or EMPTY"
}
else {
$subFolderItems = Get-ChildItem $startfolder.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
$startfolder + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
}
the output is like:
test -- 2.65 MB

Related

PowerShell to find folder / file size

I am trying to write a script using powershell to get folder / file size as mentioned below
$StartFolder = "D:\"
$Output = "C:\Temp\test-d.csv"
Add-Content -Value "Folder Path|Size" -Path $Output
$colItems = (Get-ChildItem $startFolder -Recurse | Measure-Object -Property Length -Sum)
"$StartFolder -- " + "{0:N2}" -f ($colItems.Sum / 1MB) + " MB" # | Out-File $Output
$colItems = (Get-ChildItem $startFolder -Recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems) {
$subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object -Property Length -Sum)
$i.FullName + "|" + "{0:N2}" -f ($subFolderItems.Sum / 1MB) + " MB" | Out-File $Output -Append
}
I am getting error as mentioned below:
Measure-Object : The property "Length" cannot be found in the input for any
objects.
At line:12 char:65
+ $subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object - ...
+
+ CategoryInfo : InvalidArgument: (:) [Measure-Object], PSArgumentException
+ FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand
Measure-Object : The property "Length" cannot be found in the input for any
objects.
At line:12 char:65
+ $subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object - ...
+
+ CategoryInfo : InvalidArgument: (:) [Measure-Object], PSArgumentException
+ FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand
Also, when I target C: drive I am getting "access denied" on some system files:
Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\WMI\RtBackup'
is denied.
At line:12 char:28
+ $subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object - ...
+
+ CategoryInfo : PermissionDenied: (C:\Windows\Syst...es\WMI\RtBackup:String) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
DirectoryInfo objects don't have a property Length, so you need to restrict your size calculation to files.
$colItems = Get-ChildItem $startFolder -Recurse -Force |
Where-Object { -not $_.PSIsContainer } |
Measure-Object -Property Length -Sum
It might be easier to use a Scripting.FileSystemObject COM object, because that will allow you to get directory objects with the size of their content. And you may want to use Export-Csv for exporting your data to a CSV. Use calculated properties for building the required objects.
$fso = New-Object -COM 'Scripting.FileSystemObject'
$folders = #($StartFolder)
$folders += Get-ChildItem $StartFolder -Recurse -Force |
Where-Object { $_.PSIsContainer } |
Select-Object -Expand FullName
$folders |
Select-Object #{n='Path';e={$_}}, #{n='Size';e={$fso.GetFolder($_).Size}} |
Export-Csv $Output -Delimiter '|' -NoType

Measure-Object : The property "length" cannot be found in the input for any objects

I'm creating a menu and one of the options is to report on folder size for a specified folder and display it back to the user. After I put in the folder name
cls
$Path = Read-Host -Prompt 'Please enter the folder name: '
$FolderItems = (Get-ChildItem $Path -recurse | Measure-Object -property length -sum)
$FolderSize = "{0:N2}" -f ($FolderItems.sum / 1MB) + " MB"
I get the following error:
Measure-Object : The property "length" cannot be found in the input for any objects.
At C:\Users\Erik\Desktop\powershell script.ps1:53 char:48
+ ... (Get-ChildItem $Path -recurse | Measure-Object -property length -sum)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Measure-Object], PSArgumentException
+ FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.
MeasureObjectCommand
There are no files in the folders, so you only get DirectoryInfo-objects which doesn't have a length-property. You can avoid this by filtering for files-only using:
(Get-ChildItem $Path -Recurse | Where-Object { -not $_.PSIsContainer } | Measure-Object -property length -sum)
Or for PS 3.0+
(Get-ChildItem $Path -Recurse -File | Measure-Object -property length -sum)

Adding another parameter to my script

i am very very new and i tried out a lot but i can't find a way how to add another property or something to my existing script.
i am searching for subfolder sizes and now i want to prevent the script from showing me empty folders by adding something like
| where-object size -gt 1KB OR something similar to it
i tried to put the pipeline behind others and i also tried to use an -and operator but i had no success. That's why i am asking you now :)
Thats my Script so far.
$startFolder = "C:\data1"
$colItems = (Get-ChildItem $startFolder | Measure-Object -property length -sum )
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"
$colItems = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} )
foreach ($i in $colItems)
{
$subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum -ea SilentlyContinue )
$i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
}
if($subFolderItems.Sum -gt 0)
{
$i.FullName + " -- + {0:N2} MB" -f ($subFolderItems.sum/1MB)
}

Query Folder tree for Size and export to a log on a server

Im looking to query the my documents folder for size, but also list the subfolders for size and export to a directory. I found some of this online and am trying to tweak to my needs but having issues with the export part, any help is GREATLY appreciated! :
$startFolder = "C:\Users"
$colItems = (Get-ChildItem $startFolder | Measure-Object -property length -sum)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"
add-content \\server\logs$\DirSize\log.log "$(gc env:computername)"
$colItems = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems){
{
$subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum)
$i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB" | add-content \\server\logs$\DirSize\log.log}}
Right now it is opening all folders under the C:\User\"User", can I limit to just one subdirectory under C:\Users\"User"? Thanks again!
Give this go:
$log = "C:\logfile.log"
$startFolder = "C:\Users"
$colItems = Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object
foreach ($i in $colItems){
$itemSum = Get-ChildItem ("$startFolder\" + $i.Name) -recurse | Measure-Object -property length -sum
"$startFolder\$i -- " + "{0:N2}" -f ($itemSum.sum / 1MB) + " MB" >> $log
}
This will output all the folders sizes within the given $startFolder and output the result to a log file ($log) that will look like this:
Start file: 'C:\test'
C:\test\config -- 0.00 MB
C:\test\docs -- 0.38 MB
C:\test\files -- 0.03 MB
C:\test\Help -- 7.28 MB
C:\test\inbox -- 0.00 MB
C:\test\install -- 0.00 MB
C:\test\jobs -- 0.00 MB
Stepping down 2 levels:
$log = "C:\logfile.log"
$startFolder = "C:\Users"
$colItems = Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object
foreach ($i in $colItems){
$itemSum = Get-ChildItem ("$startFolder\" + $i.Name) -recurse | Measure-Object -property length -sum
"$startFolder\$i -- " + "{0:N2}" -f ($itemSum.sum / 1MB) + " MB" >> $log
$colItems2 = Get-ChildItem ("$startFolder\" + $i.Name) | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object
foreach ($j in $colItems2){
If($j -ne $null){
$itemSum2 = Get-ChildItem ("$startFolder\" + $i.Name +"\"+ $j.Name) -recurse | Measure-Object -property length -sum
"-->$startFolder\$i\$j -- " + "{0:N2}" -f ($itemSum2.sum / 1MB) + " MB" >> $log
}
}
}
This will recurse all subdirectories of the tree and get their respective sizes:
Param($Path = $(throw "Missing directory")
,$CSVFile = $(throw "Missing logfile")
)
Function GerDirSize($PathDir)
{
#Gets subdirectories
$dirs = Get-ChildItem $PathDir | Where-Object {$_.PsIsContainer}
#Check if the list is empty
if($dirs -ne $null)
{
foreach($dir in $dirs)
{
#Gets size of current directory
$itemSum = Get-ChildItem $dir.FullName -recurse | Where-Object {!$_.PsIsContainer} | Measure-Object -Property length -Sum
$itemSum = "{0:N2}" -F ($itemSum.sum / 1MB)
$result = $dir.FullName + ";" + $itemSum
Add-Content $CSVFile -Value $result
#Gets size of subdirectories
GerDirSize($dir.FullName)
}
}
}
GerDirSize $Path
Name it GetDirSize.ps1 and use it as follows:
GetDirSize.ps1 "c:\temp" "c:\temp\logfile.log"
The result will be:
C:\temp\directory1;10.34
C:\temp\directory1\subdirectory1;2.07
C:\temp\directory1\subdirectory1\subsub1;0.00
C:\temp\directory1\subdirectory2;6.21
C:\temp\directory1\subdirectory3;0.00
C:\temp\directory2;4.14
C:\temp\directory3;10.34
C:\temp\directory3\subdirectory1;4.14
C:\temp\directory3\subdirectory1\subdirectory1;2.07
C:\temp\directory4;0.00

moving item failing on zero bytes

Where am I going wrong with the below script...it checks for if files are of zero (0) bytes and then if they are it moves them to a folder.
It works fine outside of the IF statement but when I try it in the below it fails copying the files and displays the below error:
Move-Item : Cannot bind argument to parameter 'Path' because it is
null. At C:\Tools\jon\testing_scheduled.ps1:109 char:11
+ Move-Item <<<< $moving "$scheduledpath\Move_empty"
+ CategoryInfo : InvalidData: (:) [Move-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.MoveItemCommand
filter gettheheckout([string]$path = '.')
{
$move = Get-ChildItem $scheduledpath | Where-Object {$_.length -eq 0} | Foreach-Object {$_.fullName}
}
$moving = gettheheckout
$check = #(Get-ChildItem $scheduledpath | Where-Object {$_.length -eq 0})
if ($check.length -eq 0)
{
Write-host = "No files to move - Script Completed" -ForegroundColor Cyan
}
else
{
Move-Item $moving "$scheduledpath\Move_empty"
Write-Host "Script Completed - Use Excel to Filter on commas - Have a nice day!" -ForegroundColor Cyan
}
change this:
filter gettheheckout([string]$path = '.')
{
Get-ChildItem $scheduledpath | Where-Object {$_.length -eq 0} | Foreach-Object {$_.fullName}
}
and be sure $scheduledpath is a global scope variable and have a value.