0

In the following code I'm getting "Microsoft.PowerShell.Commands.GenericObjectMeasureInfo.Minimum" for the result of the $oldest.Minimum while within the loop when running $oldest = (Get-ChildItem -Path "$_" -Name | Measure-Object -Minimum)

After the loop $oldest.Minimum returns the correct value for the last object in the loop. Additionally, if I just run Get-ChildItem -Path "$_" -Name | Measure-Object -Minimum, I get the correct output within the loop.

Any help would be appreciated.

The Code:

$folders= @()
Get-Content -Path .\folders.txt | ForEach-Object{
echo "path is $_"
$oldest = (Get-ChildItem -Path "$_" -Name | Measure-Object -Minimum)
echo "oldest folder is $oldest.Minimum"
$folders += "$_\$oldest.Minimum"
}
echo $oldest.Minimum
$minMin =($folders | Measure-Object -Minimum)
echo $minMin.Minimum

Result is:

path is C:\Local Files\Script testing\Dates\1
oldest folder is Microsoft.PowerShell.Commands.GenericObjectMeasureInfo.Minimum
path is C:\Local Files\Script testing\Dates\2
oldest folder is Microsoft.PowerShell.Commands.GenericObjectMeasureInfo.Minimum
path is C:\Local Files\Script testing\Dates\3
oldest folder is Microsoft.PowerShell.Commands.GenericObjectMeasureInfo.Minimum
2019-12-10
C:\Local Files\Script testing\Dates\1\Microsoft.PowerShell.Commands.GenericObjectMeasureInfo.Minimum

Result with Get-ChildItem -Path "$_" -Name | Measure-Object -Minimum in the loop, not storing to a varible.

path is C:\Local Files\Script testing\Dates\1


Count    : 5
Average  :
Sum      :
Maximum  :
Minimum  : 2019-12-18
Property :

oldest folder is Microsoft.PowerShell.Commands.GenericObjectMeasureInfo.Minimum
path is C:\Local Files\Script testing\Dates\2
Count    : 5
Average  :
Sum      :
Maximum  :
Minimum  : 2019-12-18
Property :

oldest folder is Microsoft.PowerShell.Commands.GenericObjectMeasureInfo.Minimum
path is C:\Local Files\Script testing\Dates\3
Count    : 5
Average  :
Sum      :
Maximum  :
Minimum  : 2019-12-10
Property :

Expected output:

path is C:\Local Files\Script testing\Dates\1
oldest folder is 2019-12-18
path is C:\Local Files\Script testing\Dates\2
oldest folder is 2019-12-18
path is C:\Local Files\Script testing\Dates\3
oldest folder is 2019-12-10
2019-12-10
C:\Local Files\Script testing\Dates\1\2019-12-10

1 Answers1

1

In your foreach loop, when you echo the statement "oldest folder is $variable.property", you are printing the $variable but not the property. Thats because .property is considered part of the string.

If you need to print the property of the variable within quoted string, you have to cover it with $() to make it an argument that needs to be evaluated. Following this solve the issue.

Get-Content -Path .\folders.txt | ForEach-Object{
  echo "path is $_"
  $oldest = (Get-ChildItem -Path "$_" -Name | Measure-Object -Minimum)
  echo "oldest folder is $($oldest.Minimum)"
  $folders += "$_\$($oldest.Minimum)"
}
Jawad
  • 11,028
  • 3
  • 24
  • 37